-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstances.py
More file actions
executable file
·172 lines (117 loc) · 3.96 KB
/
Copy pathinstances.py
File metadata and controls
executable file
·172 lines (117 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# coding: utf-8
import os
import subprocess
class InstancesManager:
def __init__(self, print_command = True, execute_command=False):
# VM arguments
self.zone = 'asia-east1-b' # TODO
self.instance_name = 'instance-ia' # TODO
self.ssh_key_file = '/Users/cecile/.ssh/google_compute_engine' # TODO
# Action arguments
self.print_command = print_command
self.execute_command = execute_command
self.status = 'UNDEFINED'
def command_application(self, command):
"""
:param command:
:param command_only:
:return:
"""
if self.print_command:
print(command)
if self.execute_command:
os.system(command)
# Check instance state : TERMINATED, STOPPING, STAGING, RUNNING
def check_state(self):
"""
:return:
"""
command = 'gcloud compute instances describe '+self.instance_name+' --zone '+self.zone \
+ ' | grep "status: RUNNING"'
status = subprocess.getoutput(command)
if status == 'status: RUNNING':
self.status = "RUNNING"
else:
self.status = "TERMINATED"
# Starts instance if not the case
def instance_init(self):
"""
:return:
"""
self.check_state()
if self.status is "TERMINATED":
self.start()
# Stops instance if not the case
def instance_final(self):
"""
:return:
"""
self.check_state()
if self.status is "RUNNING":
self.stop()
def start(self):
"""
:return:
"""
command = 'gcloud compute instances start ' + self.instance_name + ' --zone ' + self.zone
self.command_application(command)
def stop(self):
"""
:return:
"""
command = 'gcloud compute instances stop ' + self.instance_name + ' --zone ' + self.zone
self.command_application(command)
def ssh_command(self, ssh_command):
"""
:param ssh_command:
:return:
"""
command = 'gcloud compute ssh ' + self.instance_name \
+ ' --zone ' + self.zone \
+ ' --command ' + ssh_command \
+ ' --ssh-key-file ' + self.ssh_key_file
self.command_application(command)
def ssh_command_container(self, ssh_command, container_id):
"""
:param ssh_command:
:return:
"""
command = 'gcloud compute ssh ' + self.instance_name \
+ ' --zone ' + self.zone \
+ ' --command ' + ssh_command \
+ ' --ssh-key-file ' + self.ssh_key_file \
+ ' --container ' + container_id
self.command_application(command)
def scp(self, direction, src_folder, dst_folder, recurse=True, python_filter=True, pickle_filter=False,
csv_filter=False, txt_filter=False):
"""
:param direction:
:param src_folder:
:param dst_folder:
:param recurse:
:param python_filter:
:return:
"""
command = 'gcloud compute scp '
if recurse:
command = command + '--recurse '
command = command + '--zone '\
+ self.zone+' '\
if direction is 'up':
dst_folder = self.instance_name+':'+dst_folder
else:
src_folder = self.instance_name+':'+src_folder
# Adding source folder
command = command + src_folder
if python_filter:
command = command + '/*.py'
if pickle_filter:
command = command + '/*.pkl'
if csv_filter:
command = command + '/*.csv'
if txt_filter:
command = command + '/*.txt'
# Adding destination folder
command = command + ' ' + dst_folder
command = command + ' --ssh-key-file ' + self.ssh_key_file
self.command_application(command)