Skip to content

Commit 57c3c11

Browse files
committed
adding disgusting function to parse ip address
Signed-off-by: Vanessa Sochat <vsochat@stanford.edu>
1 parent 76e13a5 commit 57c3c11

File tree

5 files changed

+96
-6
lines changed

5 files changed

+96
-6
lines changed

scompose/client/ps.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
'''bring one or more instances down
31+
'''
32+
# Initialize the project
33+
project = Project(filename=args.file,
34+
name=args.project_name,
35+
env_file=args.env_file)
36+
37+
# Create instances, and if none specified, create all
38+
project.ps()

scompose/client/up.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ def main(args, parser, extra):
3838
env_file=args.env_file)
3939

4040
# Create instances, and if none specified, create all
41-
project.up(args.names, writable_tmpfs=writable_tmpfs)
41+
project.up(args.names, writable_tmpfs=args.writable_tmpfs)

scompose/project/instance.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,13 @@ def exists(self):
258258
'''return boolean if an instance exists. We do this by way of listing
259259
instances, and so the calling user is important.
260260
'''
261-
instances = [x.name for x in self.client.instances(quiet=True)]
261+
instances = [x.name for x in self.client.instances(quiet=True, sudo=self.sudo)]
262262
return self.name in instances
263263

264264
def get(self):
265265
'''If an instance exists, add to self.instance
266266
'''
267-
for instance in self.client.instances(quiet=True):
267+
for instance in self.client.instances(quiet=True, sudo=self.sudo):
268268
if instance.name == self.name:
269269
self.instance = instance
270270
break
@@ -275,7 +275,7 @@ def stop(self):
275275
'''
276276
if self.instance:
277277
bot.info("Stopping %s" % self)
278-
self.instance.stop()
278+
self.instance.stop(sudo=self.sudo)
279279
self.instance = None
280280

281281
# Create and Delete

scompose/project/project.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@
1717
1818
'''
1919

20+
from scompose.templates import get_template
2021
from scompose.logger import bot
21-
from scompose.utils import read_yaml
22+
from scompose.utils import (
23+
read_yaml,
24+
read_file,
25+
write_file
26+
)
2227
from spython.main import get_client
2328
from .instance import Instance
2429
import json
@@ -158,6 +163,50 @@ def parse(self):
158163
for _, instance in self.instances.items():
159164
instance.set_volumes_from(self.instances)
160165

166+
# Networking
167+
168+
169+
def create_hosts(self, name, depends_on):
170+
'''create a hosts file to bind to all containers, where we define the
171+
correct hostnames to correspond with the ip addresses created.
172+
173+
Note: This function is terrible. Singularity should easily expose
174+
these addresses.
175+
'''
176+
template = read_file(get_template('hosts'))
177+
hosts_file = os.path.join(self.working_dir, 'etc.hosts.%s' % name)
178+
hosts_basename = os.path.basename(hosts_file)
179+
180+
for _, instance in self.instances.items():
181+
if instance.name in depends_on:
182+
if self.sudo:
183+
if instance.exists():
184+
result = self.client.execute(image=instance.instance.get_uri(),
185+
command=['hostname', '-I'],
186+
return_result=True,
187+
sudo=self.sudo)
188+
189+
# Busybox won't have hostname -I
190+
if result['return_code'] != 0:
191+
cmd = "ip -4 --oneline address show up eth0"
192+
result = self.client.execute(image=instance.instance.get_uri(),
193+
command=cmd,
194+
return_result=True,
195+
sudo=self.sudo)
196+
197+
ip_address = result['message'].strip('\n').strip()
198+
199+
# Clean up busybox output
200+
if "inet" in ip_address:
201+
ip_address = re.match('.+ inet (?P<address>.+)/', ip_address).groups()[0]
202+
else:
203+
ip_address = '127.0.1.1'
204+
205+
template = ['%s\t%s\n' % (ip_address, instance.name)] + template
206+
instance.volumes.append('%s:/etc/hosts' % hosts_basename)
207+
write_file(hosts_file, template)
208+
209+
161210
# Commands
162211

163212
def shell(self, name):
@@ -235,6 +284,9 @@ def _create(self, names, command="create", writable_tmpfs=False):
235284
count += 1
236285
continue
237286

287+
# Create a hosts file for the instance based on depends
288+
self.create_hosts(instance.name, created)
289+
238290
# If we get here, execute command and add to list
239291
getattr(instance, command)(self.working_dir, writable_tmpfs)
240292
created.append(instance.name)

scompose/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232

3333
INSTALL_REQUIRES = (
34-
('spython', {'min_version': '0.0.34'}),
34+
('spython', {'min_version': '0.0.67'}),
3535
)
3636

3737
INSTALL_REQUIRES_ALL = INSTALL_REQUIRES

0 commit comments

Comments
 (0)