Skip to content
This repository has been archived by the owner on Oct 1, 2022. It is now read-only.

Commit

Permalink
remove and deactivate environment
Browse files Browse the repository at this point in the history
remove service
  • Loading branch information
eromano committed Jan 5, 2018
1 parent dc52880 commit 471177e
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.idea/
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,54 @@ Options:
--timeout How many seconds to wait until get back prompt on deactivation
```

### remove command

```
Usage: ./gaucho remove <service_id>
Remove the given *service id*.
Required Arguments:
service_id The ID of the service to perform the command on.
Options:
--timeout How many seconds to wait until get back prompt on remove
```

### deactivate environment command

```
Usage: ./gaucho deactivate_env <environment_id>
Deactivate the given *environment id*.
Required Arguments:
environment_id The ID of the environment to perform the command on.
Options:
--timeout How many seconds to wait until get back prompt on deactivation
```

### remove environment command

```
Usage: ./gaucho remove_env <environment_id>
Remove the given *environment id*.
Required Arguments:
environment_id The ID of the environment to perform the command on.
Options:
--timeout How many seconds to wait until get back prompt on remove
```

### Get service's state

```
Expand Down
111 changes: 107 additions & 4 deletions services.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

HOST = "http://rancher.local:8080/v1"
URL_SERVICE = "/services/"
URL_ENVIRONMENT = "/projects/"
USERNAME = "userid"
PASSWORD = "password"
kwargs = {}
Expand All @@ -28,6 +29,14 @@ def post(url, data=""):
r.raise_for_status()
return r.json()

def delete(url, data=""):
if data:
r = requests.delete(url, data=json.dumps(data), auth=(USERNAME, PASSWORD), **kwargs)
else:
r = requests.delete(url, data="", auth=(USERNAME, PASSWORD), **kwargs)
r.raise_for_status()
return r.json()

# Websocket
def ws(url):
webS = websocket.create_connection(url)
Expand Down Expand Up @@ -67,6 +76,17 @@ def id_of (name=""):
service = get(HOST + "/services?name=" + name).json()
return service['data'][0]['id']

#
# Converts a environment name into an ID
#
@baker.command(params={"name": "The name of the environment to lookup."})
def id_of_env (name=""):
"""Retrieves the ID of a project, given its name.
"""

environment = get(HOST + "/project?name=" + name).json()
return environment['data'][0]['id']

#
# Start containers within a service (e.g. for Start Once containers).
#
Expand Down Expand Up @@ -329,7 +349,7 @@ def rollback(service_id, timeout=60):
#
# Activate a service.
#
@baker.command(params={"service_id": "The ID of the service to deactivate.",
@baker.command(params={"service_id": "The ID of the service to activate.",
"timeout": "How many seconds to wait until an upgrade fails"})
def activate (service_id, timeout=60):
"""Activate the containers of a given service.
Expand All @@ -338,14 +358,14 @@ def activate (service_id, timeout=60):
r = get(HOST + URL_SERVICE + service_id)
current_service_config = r.json()

# can't deactivate a service if it's not in active state
# can't activate a service if it's not in inactive state
if current_service_config['state'] != "inactive":
print "Service cannot be deactivated due to its current state: %s" % current_service_config['state']
sys.exit(1)

post(current_service_config['actions']['activate'], "");

# Wait deactivation to finish
# Wait Activation to finish
sleep_count = 0
while current_service_config['state'] != "active" and sleep_count < timeout // 2:
print "Waiting for activation to finish..."
Expand All @@ -368,7 +388,7 @@ def deactivate (service_id, timeout=60):
current_service_config = r.json()

# can't deactivate a service if it's not in active state
if current_service_config['state'] != "active":
if current_service_config['state'] != "active" and current_service_config['state'] != "updating-active":
print "Service cannot be deactivated due to its current state: %s" % current_service_config['state']
sys.exit(1)

Expand All @@ -383,6 +403,89 @@ def deactivate (service_id, timeout=60):
current_service_config = r.json()
sleep_count += 1

#
# Deactivate a env.
#
@baker.command(params={"environment_id": "The ID of the environment to deactivate.",
"timeout": "How many seconds to wait until an upgrade fails"})
def deactivate_env (environment_id, timeout=60):
"""Stops the environment
"""

r = get(HOST + URL_ENVIRONMENT + environment_id )
current_environment_config = r.json()

# can't deactivate a service if it's not in active state
if current_environment_config['state'] != "active":
print "Environment cannot be deactivated due to its current state: %s" % current_environment_config['state']
sys.exit(1)

post(current_environment_config['actions']['deactivate'], "");

# Wait deactivation to finish
sleep_count = 0
while current_environment_config['state'] != "inactive" and sleep_count < timeout // 2:
print "Waiting for deactivation to finish..."
time.sleep (2)
r = get(HOST + URL_ENVIRONMENT + environment_id)
current_environment_config = r.json()
sleep_count += 1

#
# Delete a env.
#
@baker.command(params={"environment_id": "The ID of the environment to delete.",
"timeout": "How many seconds to wait until an upgrade fails"})
def delete_env (environment_id, timeout=60):
"""Stops the environment
"""

r = get(HOST + URL_ENVIRONMENT + environment_id )
current_environment_config = r.json()

# can't deactivate a service if it's not in active state
if current_environment_config['state'] != "inactive":
print "Environment cannot be deactivated due to its current state: %s" % current_environment_config['state']
sys.exit(1)

delete(current_environment_config['actions']['delete'], "");

# Wait deactivation to finish
sleep_count = 0
while current_environment_config['state'] != "removed" and sleep_count < timeout // 2:
print "Waiting for delete to finish..."
time.sleep (2)
r = get(HOST + URL_ENVIRONMENT + environment_id)
current_environment_config = r.json()
sleep_count += 1

#
# Remove a service.
#
@baker.command(params={"service_id": "The ID of the service to remove.",
"timeout": "How many seconds to wait until an upgrade fails"})
def remove (service_id, timeout=60):
"""Remove the service
"""

r = get(HOST + URL_SERVICE + service_id)
current_service_config = r.json()

# can't remove a service if it's not in inactive state
if current_service_config['state'] != "inactive":
print "Service cannot be removed due to its current state: %s" % current_service_config['state']
sys.exit(1)

post(current_service_config['actions']['remove'], "");

# Wait remove to finish
sleep_count = 0
while current_service_config['state'] != "removed" and sleep_count < timeout // 2:
print "Waiting for remove to finish..."
time.sleep (2)
r = get(HOST + URL_SERVICE + service_id)
current_service_config = r.json()
sleep_count += 1


#
Expand Down

0 comments on commit 471177e

Please sign in to comment.