Skip to content

Commit

Permalink
Agent: Add stop and complete lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
wallento committed May 30, 2017
1 parent 2f1c6a6 commit ac89c14
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
36 changes: 35 additions & 1 deletion lcci/agent.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

import docker
import docker, requests

class Agent(object):
"""docstring for Agent."""
Expand Down Expand Up @@ -29,12 +29,46 @@ def start(self,no_daemon=False):

env = { "AGENT_ID": self.id, "AGENT_SECRET": self.secret }

try:
cont = client.containers.get("lcci-{}".format(self.name))
logging.warn("Container already/yet exists")
self.stop()
except docker.errors.NotFound:
pass

logging.info("Start the container")

client.containers.run(
"librecores/ci-{}-agent".format(self.type),
name = "lcci-{}".format(self.name),
devices = self.devices,
environment = env,
detach = not no_daemon,
volumes = self.volumes
)

def stop(self):
logging.info("Stop agent {}".format(self.name))

client = docker.from_env(version='auto')

try:
cont = client.containers.get("lcci-{}".format(self.name))
except:
logging.error("Cannot find container")
exit(1)

try:
cont.stop()
except docker.errors.APIError as e:
if (e.status_code() == 137):
logging.error("Container cannot be stopped" + e)
exit(1)
except requests.exceptions.ReadTimeout:
cont.kill()

try:
cont.remove()
except docker.errors.APIError as e:
logging.error("Container cannot be removed")
exit(1)
15 changes: 15 additions & 0 deletions lcci/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ def agent_start(args):

agent.start(args.no_daemon)

def agent_stop(args):
conf = Configuration(args)

try:
agent = conf.agents[args.agent]
except KeyError:
logging.error("Cannot find agent {}".format(args.agent))
exit(1)

agent.stop()

def main():
logging.basicConfig(level=logging.INFO)

Expand All @@ -43,6 +54,10 @@ def main():
parser_agent_start.add_argument("--no-daemon", action='store_true')
parser_agent_start.set_defaults(func=agent_start)

parser_agent_start = subparsers_agent.add_parser('stop', help='Stop agent')
parser_agent_start.add_argument("agent")
parser_agent_start.set_defaults(func=agent_stop)

parsed_args = parser.parse_args()
if hasattr(parsed_args, 'func'):
parsed_args.func(parsed_args)
Expand Down

0 comments on commit ac89c14

Please sign in to comment.