I wrote some small code for controlling multiple compute engine on google cloud platform. The whole file is here in github.
The part causes problem is gcloud compute ssh in the following
def upload_file(self, projectname):
var = raw_input("Upload file with name: " + projectname + " will remove all file and directory with same name in the instance. Are you sure? (y/n)")
if var.lower().strip() != "y":
print "Abort uploading."
return
is_zip = False
if projectname.split('.')[1] == "zip":
is_zip = True
fullpath_projectname = __location__ + "/" + projectname
if os.path.isfile(fullpath_projectname):
all_instances = self.search_any('instances', filter="labels.type=" + self.working_group_label)
all_instances_name = [x['name'] for x in all_instances]
i = 0
for instance in all_instances_name:
temp_command = 'gcloud_command_' + str(i)
i += 1
temp_instance = user_name + "@" + instance
temp_file_path = '/home/' + user_name + '/'
command_arr = []
command_arr.append('call gcloud compute ssh ' + temp_instance + ' --zone ' + zone + ' --command "cd ' + temp_file_path + '; sudo rm -rf ' + temp_file_path + projectname.split('.')[0] + '; sudo rm -f ' + temp_file_path + projectname.split('.')[0] + '*"\n')
command_arr.append('call gcloud compute scp "' + fullpath_projectname + '" ' + temp_instance + ':' + temp_file_path + '\n')
if is_zip:
command_arr.append('call gcloud compute ssh ' + temp_instance + ' --zone ' + zone + ' ' + ' --command "cd ' + temp_file_path + '; sudo unzip ' + temp_file_path + projectname + '"' + '\n')
with open(os.path.join(__location__, 'bat/' + temp_command + '.bat'), 'w') as bat:
bat.writelines(command_arr)
subprocess.Popen(os.path.join(__location__, 'bat/' + temp_command + '.bat'), shell=True)
This part is for uploading the same file (usually .zip) to all instances with same tag. However doing it on my local computer will jump out a lot of alert asking me for saving the ssh fingerprint.
Like this,
Originally it is a small tool for my own convenience, in order to run multiple compute engine to query online data (I want to have multiple IP so as to avoid being classified as DDoS). But now I need to share this with other teams so that bundle of alert causes other's confusing.
I can cache it one by one manually, but it is not convenient for others.
I tried to answer the caching by changing last line
proc = subprocess.Popen(os.path.join(__location__, 'bat/' + temp_command + '.bat'), shell=True)
proc.communicate(input='y')
But this make all upload just do one by one and that's extremely slow for many instances.
