File: power.py

package info (click to toggle)
python-softlayer 6.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,508 kB
  • sloc: python: 57,195; makefile: 133; xml: 97; sh: 59
file content (109 lines) | stat: -rw-r--r-- 3,503 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""Command lines which modify power states."""
# :license: MIT, see LICENSE for more details.

import click

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import exceptions
from SoftLayer.CLI import formatting
from SoftLayer.CLI import helpers


@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.argument('identifier')
@environment.pass_env
def rescue(env, identifier):
    """Reboot into a rescue image."""

    vsi = SoftLayer.VSManager(env.client)
    vs_id = helpers.resolve_id(vsi.resolve_ids, identifier, 'VS')
    if not (env.skip_confirmations or
            formatting.confirm("This action will reboot this VSI. Continue?")):
        raise exceptions.CLIAbort('Aborted')

    vsi.rescue(vs_id)


@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.argument('identifier')
@click.option('--hard/--soft',
              default=None,
              help="Perform a hard or soft reboot")
@environment.pass_env
def reboot(env, identifier, hard):
    """Reboot an active virtual server."""

    virtual_guest = env.client['Virtual_Guest']
    mgr = SoftLayer.VSManager(env.client)
    vs_id = helpers.resolve_id(mgr.resolve_ids, identifier, 'VS')
    if not (env.skip_confirmations or
            formatting.confirm(f'This will reboot the VS with id {vs_id}. '
                               'Continue?')):
        raise exceptions.CLIAbort('Aborted.')

    if hard is True:
        virtual_guest.rebootHard(id=vs_id)
    elif hard is False:
        virtual_guest.rebootSoft(id=vs_id)
    else:
        virtual_guest.rebootDefault(id=vs_id)


@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.argument('identifier')
@click.option('--hard/--soft', help="Perform a hard shutdown")
@environment.pass_env
def power_off(env, identifier, hard):
    """Power off an active virtual server."""

    virtual_guest = env.client['Virtual_Guest']
    vsi = SoftLayer.VSManager(env.client)
    vs_id = helpers.resolve_id(vsi.resolve_ids, identifier, 'VS')
    if not (env.skip_confirmations or
            formatting.confirm(f'This will power off the VS with id {vs_id}. '
                               'Continue?')):
        raise exceptions.CLIAbort('Aborted.')

    if hard:
        virtual_guest.powerOff(id=vs_id)
    else:
        virtual_guest.powerOffSoft(id=vs_id)


@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.argument('identifier')
@environment.pass_env
def power_on(env, identifier):
    """Power on a virtual server."""

    vsi = SoftLayer.VSManager(env.client)
    vs_id = helpers.resolve_id(vsi.resolve_ids, identifier, 'VS')
    env.client['Virtual_Guest'].powerOn(id=vs_id)


@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.argument('identifier')
@environment.pass_env
def pause(env, identifier):
    """Pauses an active virtual server."""

    vsi = SoftLayer.VSManager(env.client)
    vs_id = helpers.resolve_id(vsi.resolve_ids, identifier, 'VS')

    if not (env.skip_confirmations or
            formatting.confirm(f'This will pause the VS with id {vs_id}. Continue?')):
        raise exceptions.CLIAbort('Aborted.')

    env.client['Virtual_Guest'].pause(id=vs_id)


@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.argument('identifier')
@environment.pass_env
def resume(env, identifier):
    """Resumes a paused virtual server."""

    vsi = SoftLayer.VSManager(env.client)
    vs_id = helpers.resolve_id(vsi.resolve_ids, identifier, 'VS')
    env.client['Virtual_Guest'].resume(id=vs_id)