Skip to content

Instantly share code, notes, and snippets.

@shiruta shiruta/GCF_GVISOR_EVAL_python.md Secret
Last active May 29, 2018

Embed
What would you like to do?

deploy function

gcloud alpha functions deploy hello_http --trigger-http --runtime python37

exec function

$ curl  -X POST -H "Content-Type:application/json" -d '{"exec_cmd":"uname -a"}'  https://us-central1-${PROJECT_NAME}.cloudfunctions.net/hello_http
Linux localhost 3.11.10 #1 SMP Fri Nov 29 10:47:50 PST 2013 x86_64 x86_64 x86_64 GNU/Linux
$ curl  -X POST -H "Content-Type:application/json" -d '{"exec_cmd":"uptime"}'  https://us-central1-${PROJECT_NAME}.cloudfunctions.net/hello_http
12:52:09 up 2 min, 0 users, load average: 0.00, 0.00, 0.00.
$ curl  -X POST -H "Content-Type:application/json" -d '{"exec_cmd":"cat /proc/cpuinfo"}'  https://us-central1-${PROJECT_NAME}.cloudfunctions.net/hello_http
 power management:. 
 address sizes	: 46 bits physical, 48 bits virtual. 
 cache_alignment	: 64. 
 clflush size	: 64. 
 bogomips	: 2200.00. 
 flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt rdseed adx smap xsaveopt. 
 wp		: yes. 
 cpuid level	: 13. 
 fpu_exception	: yes. 
 fpu		: yes. 
 cpu MHz		: 2200.000. 
 stepping	: unknown. 
 model name	: unknown. 
 model		: 79. 
 cpu family	: 6. 
 vendor_id	: GenuineIntel. 
 processor	: 7. 
 power management:. 
 address sizes	: 46 bits physical, 48 bits virtual. 
 cache_alignment	: 64. 
 clflush size	: 64. 
 bogomips	: 2200.00. 
 flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt rdseed adx smap xsaveopt. 
 wp		: yes. 
 cpuid level	: 13. 
 fpu_exception	: yes. 
 fpu		: yes. 
 cpu MHz		: 2200.000. 
 stepping	: unknown. 
 model name	: unknown. 
 model		: 79. 
 cpu family	: 6. 
 vendor_id	: GenuineIntel. 
 processor	: 6. 
 power management:. 
 address sizes	: 46 bits physical, 48 bits virtual. 
 cache_alignment	: 64. 
 clflush size	: 64. 
 bogomips	: 2200.00. 
 flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt rdseed adx smap xsaveopt. 
 wp		: yes. 
 cpuid level	: 13. 
 fpu_exception	: yes. 
 fpu		: yes. 
 cpu MHz		: 2200.000. 
 stepping	: unknown. 
 model name	: unknown. 
 model		: 79. 
 cpu family	: 6. 
 vendor_id	: GenuineIntel. 
 processor	: 5. 
 power management:. 
 address sizes	: 46 bits physical, 48 bits virtual. 
 cache_alignment	: 64. 
 clflush size	: 64. 
 bogomips	: 2200.00. 
 flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt rdseed adx smap xsaveopt. 
 wp		: yes. 
 cpuid level	: 13. 
 fpu_exception	: yes. 
 fpu		: yes. 
 cpu MHz		: 2200.000. 
 stepping	: unknown. 
 model name	: unknown. 
 model		: 79. 
 cpu family	: 6. 
 vendor_id	: GenuineIntel. 
 processor	: 4. 
 power management:. 
 address sizes	: 46 bits physical, 48 bits virtual. 
 cache_alignment	: 64. 
 clflush size	: 64. 
 bogomips	: 2200.00. 
 flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt rdseed adx smap xsaveopt. 
 wp		: yes. 
 cpuid level	: 13. 
 fpu_exception	: yes. 
 fpu		: yes. 
 cpu MHz		: 2200.000. 
 stepping	: unknown. 
 model name	: unknown. 
 model		: 79. 
 cpu family	: 6. 
 vendor_id	: GenuineIntel. 
 processor	: 3. 
 power management:. 
 address sizes	: 46 bits physical, 48 bits virtual. 
 cache_alignment	: 64. 
 clflush size	: 64. 
 bogomips	: 2200.00. 
 flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt rdseed adx smap xsaveopt. 
 wp		: yes. 
 cpuid level	: 13. 
 fpu_exception	: yes. 
 fpu		: yes. 
 cpu MHz		: 2200.000. 
 stepping	: unknown. 
 model name	: unknown. 
 model		: 79. 
 cpu family	: 6. 
 vendor_id	: GenuineIntel. 
 processor	: 2. 
 power management:. 
 address sizes	: 46 bits physical, 48 bits virtual. 
 cache_alignment	: 64. 
 clflush size	: 64. 
 bogomips	: 2200.00. 
 flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt rdseed adx smap xsaveopt. 
 wp		: yes. 
 cpuid level	: 13. 
 fpu_exception	: yes. 
 fpu		: yes. 
 cpu MHz		: 2200.000. 
 stepping	: unknown. 
 model name	: unknown. 
 model		: 79. 
 cpu family	: 6. 
 vendor_id	: GenuineIntel. 
 processor	: 1. 
 power management:. 
 address sizes	: 46 bits physical, 48 bits virtual. 
 cache_alignment	: 64. 
 clflush size	: 64. 
 bogomips	: 2200.00. 
 flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt rdseed adx smap xsaveopt. 
 wp		: yes. 
 cpuid level	: 13. 
 fpu_exception	: yes. 
 fpu		: yes. 
 cpu MHz		: 2200.000. 
 stepping	: unknown. 
 model name	: unknown. 
 model		: 79. 
 cpu family	: 6. 
 vendor_id	: GenuineIntel. 
 processor	: 0. 
# curl  -X POST -H "Content-Type:application/json" -d '{"exec_cmd":"cat /proc/meminfo"}'  https://us-central1-${PROJECT_NAME}.cloudfunctions.net/hello_http
Shmem:                 0 kB. 
Mapped:            36364 kB. 
AnonPages:         39788 kB. 
Writeback:             0 kB. 
Dirty:                 0 kB. 
SwapFree:              0 kB. 
SwapTotal:             0 kB. 
Mlocked:               0 kB. 
Unevictable:           0 kB. 
Inactive(file):    18184 kB. 
Active(file):      18180 kB. 
Inactive(anon):        0 kB. 
Active(anon):      39788 kB. 
Inactive:          18184 kB. 
Active:            57968 kB. 
SwapCache:             0 kB. 
Cached:            36364 kB. 
Buffers:               0 kB. 
MemAvailable:    2020988 kB. 
MemFree:         2020988 kB. 
MemTotal:        2097152 kB. 
$ curl  -X POST -H "Content-Type:application/json" -d '{"exec_cmd":"cat /proc/filesystems"}'  https://us-central1-${PROJECT_NAME}.cloudfunctions.net/hello_http
 nodev	tmpfs. 
nodev	sysfs. 
nodev	ramdiskfs. 
nodev	proc. 
nodev	devtmpfs. 
nodev	9p. 
43.31 0.00
$ curl  -X POST -H "Content-Type:application/json" -d '{"exec_cmd":"cat /proc/stat"}'  https://us-central1-${PROJECT_NAME}.cloudfunctions.net/hello_http
 processes 0. 
btime 1527517080. 
ctxt 0. 
intr 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0. 
cpu7 0 0 0 0 0 0 0 0 0 0. 
cpu6 0 0 0 0 0 0 0 0 0 0. 
cpu5 0 0 0 0 0 0 0 0 0 0. 
cpu4 0 0 0 0 0 0 0 0 0 0. 
cpu3 0 0 0 0 0 0 0 0 0 0. 
cpu2 0 0 0 0 0 0 0 0 0 0. 
cpu1 0 0 0 0 0 0 0 0 0 0. 
cpu0 0 0 0 0 0 0 0 0 0 0. 
cpu  0 0 0 0 0 0 0 0 0 0. 
import sys
import subprocess
from google.cloud import logging as cloudlogging
from google.cloud.logging.resource import Resource as cloudlogging_resource
client = cloudlogging.Client()
client.setup_logging()
log_name = 'gcf-python-log'
logger = client.logger(log_name)
function_name = 'hello-http-function'
resource = cloudlogging_resource(type='cloud_function',
labels={'function_name': function_name})
def command(cmd):
try:
result = subprocess.run(cmd, shell=True, check=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
universal_newlines=True)
for line in result.stdout.splitlines():
yield line
except subprocess.CalledProcessError:
print('外部プログラムの実行に失敗しました [' + cmd + ']', file=sys.stderr)
sys.exit(1)
def hello_http(request):
"""HTTP Cloud Function.
Args:
request (flask.Request): The request object.
Returns:
The response text, or any set of values that can be tuned into a
Response object using `make_response
<http://flask.pocoo.org/docs/0.12/api/#flask.Flask.make_response>`.
"""
request_json = request.get_json()
if request_json and 'exec_cmd' in request_json:
exec_cmd = request_json['exec_cmd']
for line in command(exec_cmd):
logger.log_text(f" {line}.", resource=resource)
return f'Hello, {exec_cmd}! '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.