Skip to content

Commit 10d4e44

Browse files
committed
adding exec
Signed-off-by: Vanessa Sochat <[email protected]>
1 parent 10ef73f commit 10d4e44

File tree

2 files changed

+72
-7
lines changed

2 files changed

+72
-7
lines changed

scompose/client/exec.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'''
2+
3+
Copyright (C) 2019 Vanessa Sochat.
4+
5+
This program is free software: you can redistribute it and/or modify it
6+
under the terms of the GNU Affero General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or (at your
8+
option) any later version.
9+
10+
This program is distributed in the hope that it will be useful, but WITHOUT
11+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
13+
License for more details.
14+
15+
You should have received a copy of the GNU Affero General Public License
16+
along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
'''
19+
20+
from scompose.project import Project
21+
import logging
22+
import json
23+
import sys
24+
import os
25+
26+
27+
log = logging.getLogger(__name__)
28+
29+
def main(args, parser, extra):
30+
'''execute a command to an instance.
31+
'''
32+
# Initialize the project
33+
project = Project(filename=args.file,
34+
name=args.project_name,
35+
env_file=args.env_file)
36+
37+
project.execute(args.name[0], extra)

scompose/project/project.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ def create_hosts(self, name, depends_on):
195195

196196
def shell(self, name):
197197
'''if an instance exists, shell into it.
198+
199+
Parameters
200+
==========
201+
name: the name of the instance to shell into
198202
'''
199203
if self.instances:
200204
if name in self.instances:
@@ -203,6 +207,25 @@ def shell(self, name):
203207
self.client.shell(instance.instance.get_uri(), sudo=self.sudo)
204208

205209

210+
def execute(self, name, commands):
211+
'''if an instance exists, execute a command to it.
212+
213+
Parameters
214+
==========
215+
name: the name of the instance to exec to
216+
commands: a list of commands to issue
217+
'''
218+
if self.instances:
219+
if name in self.instances:
220+
instance = self.instances[name]
221+
if instance.exists():
222+
for line in self.client.execute(instance.instance.get_uri(),
223+
command=commands,
224+
stream=True,
225+
sudo=self.sudo):
226+
print(line, end='')
227+
228+
206229
def logs(self, names, tail=0):
207230
'''logs will print logs to the screen.
208231
'''
@@ -275,19 +298,24 @@ def _create(self, names, command="create", writable_tmpfs=False):
275298

276299
for instance in self.iter_instances(names):
277300

301+
# Flag to indicated create
302+
do_create = True
303+
278304
# Ensure created, skip over if not
279305
for depends_on in instance.params.get('depends_on', []):
280306
if depends_on not in created:
281307
count += 1
282-
continue
308+
do_create = False
309+
310+
if do_create:
283311

284-
# Create a hosts file for the instance based on depends
285-
self.create_hosts(instance.name, created)
312+
# Create a hosts file for the instance based on depends
313+
self.create_hosts(instance.name, created)
286314

287-
# If we get here, execute command and add to list
288-
getattr(instance, command)(self.working_dir, writable_tmpfs)
289-
created.append(instance.name)
290-
names.remove(instance.name)
315+
# If we get here, execute command and add to list
316+
getattr(instance, command)(self.working_dir, writable_tmpfs)
317+
created.append(instance.name)
318+
names.remove(instance.name)
291319

292320
# Possibly circular dependencies
293321
if count >= 100:

0 commit comments

Comments
 (0)