Skip to content

Commit

Permalink
feat: ps:kill command
Browse files Browse the repository at this point in the history
feat: ps:kill command
  • Loading branch information
gigatim authored Apr 1, 2024
2 parents 9ea64af + 846cee1 commit 7450340
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v3
Expand Down
14 changes: 14 additions & 0 deletions gigalixir/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1229,3 +1229,17 @@ def app_maintenance_off(ctx, app_name, yes):
"""
if yes or click.confirm('Do you want to remove your app (%s) from maintenance mode?' % app_name):
gigalixir_app.maintenance(ctx.obj['host'], app_name, False)

@cli.command(name='ps:kill')
@click.option('-a', '--app_name', envvar="GIGALIXIR_APP")
@click.option('-p', '--pod', required=True, help='The name of the pod to kill.')
@click.option('-y', '--yes', is_flag=True)
@click.pass_context
@report_errors
@detect_app_name
def ps_kill(ctx, app_name, pod, yes):
"""
Kills a pod.
"""
if yes or click.confirm('Do you want to kill your pod (%s)?' % pod):
gigalixir_app.kill_pod(ctx.obj['host'], app_name, pod)
12 changes: 12 additions & 0 deletions gigalixir/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ def status(host, app_name):
data = json.loads(r.text)["data"]
presenter.echo_json(data)

def kill_pod(host, app_name, pod_name):
url = '%s/api/apps/%s/pods/%s' % (host, quote(app_name.encode('utf-8')), quote(pod_name.encode('utf-8')))
r = requests.delete(url, headers = { 'Content-Type': 'application/json' })

if r.status_code != 202:
if r.status_code == 401:
raise auth.AuthException()
raise Exception(r.text)
else:
data = json.loads(r.text)["data"]
presenter.echo_json(data)

def scale(host, app_name, replicas, size):
body = {}
if replicas != None:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
url='https://github.com/gigalixir/gigalixir-cli',
author='Tim Knight',
author_email='[email protected]',
version='1.10.0',
version='1.11.1',
packages=find_packages(),
include_package_data=True,
install_requires=[
Expand Down
18 changes: 18 additions & 0 deletions test/test_gigalixir.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import httpretty
import platform
import json
from six.moves.urllib.parse import quote

def netrc_name():
if platform.system().lower() == 'windows':
Expand Down Expand Up @@ -775,3 +776,20 @@ def test_maintenance_off():
assert result.exit_code == 0
expect(httpretty.has_request()).to.be.true
expect(httpretty.last_request().body.decode()).to.equal('{"enable": false}')

@httpretty.activate
def test_kill_pod():
httpretty.register_uri(httpretty.DELETE, 'https://api.gigalixir.com/api/apps/fake-app-name/pods/pod-abcd', body='{"data": "Pod pod-abcd is being deleted."}', content_type='application/json', status=202)

runner = CliRunner()
result = runner.invoke(gigalixir.cli, ['ps:kill', '-a', 'fake-app-name', '-p', 'pod-abcd'], input="y\n")
assert result.exit_code == 0
expect(httpretty.has_request()).to.be.true

@httpretty.activate
def test_kill_pod_404():
httpretty.register_uri(httpretty.DELETE, 'https://api.gigalixir.com/api/apps/fake-app-name/pods/pod-abcd', body='{"data": "Pod `pod-abcd` not found."}', content_type='application/json', status=404)
runner = CliRunner()
result = runner.invoke(gigalixir.cli, ['ps:kill', '-a', 'fake-app-name', '-p', 'pod-abcd'], input="y\n")
assert result.exit_code != 0
expect(httpretty.has_request()).to.be.true

0 comments on commit 7450340

Please sign in to comment.