I have a compute engine already set up, I can use ssh command in command prompt, like
gcloud compute ssh test-instance@cloud-engine-noapi --zone us-central1-f --command "rm -f test.txt"
and successfully delete the test.txt in server.
However, when I call these in python.
subprocess.call(['gcloud', 'compute', '--project', 'test-project','ssh', temp_instance, '--zone', zone, '--command', '"cd /home"'], shell=True)
subprocess.call(['gcloud', 'compute', '--project', 'test-project','ssh', temp_instance, '--zone', zone, '--command', '"ls -l"'], shell=True)
subprocess.call(['gcloud', 'compute', '--project', 'test-project','ssh', temp_instance, '--zone', zone, '--command', '"rm -f /home/test.txt"'], shell=True)
The return is always like
bash: /command : No such file or directory
and for command like ls
bash: /command : command not found
Is there any process I have to do first?
Though, probably no one has this problem... but I finally come up with a method to solve it.
As the problem only occurs when using subprocess, I bypass it by writing a (bat/sh) file to temporarily save the commands.
Like,
with open(os.path.join(__location__, 'gcloud_command.bat'), 'w') as bat:
command_arr = []
for instance in all_instances_name:
temp_instance = user_name + "@" + instance
temp_file_path = '/home/' + user_name + '/'
command_arr.append('call gcloud compute ssh ' + temp_instance + ' --zone ' + zone + ' --command "cd ' + temp_file_path + '; rm -rf ' + temp_file_path + projectname.split('.')[0] + '; rm -f ' + temp_file_path + projectname.split('.')[0] + '*"\n')
command_arr.append('call gcloud compute scp "' + fullpath_projectname + '" ' + instance + ':' + temp_file_path + '\n')
if is_zip:
command_arr.append('call gcloud compute ssh ' + temp_instance + ' --zone ' + zone + ' ' + ' --command "cd ' + temp_file_path + '; unzip ' + temp_file_path + projectname + '"' + '\n')
bat.writelines(command_arr)
And execute with
subprocess.Popen(os.path.join(__location__, 'gcloud_command.bat'))