Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #15 from charmed-kubernetes/akd/just-black
Browse files Browse the repository at this point in the history
Just Black ⚫ 🐈‍⬛ 🏴 🖤
  • Loading branch information
kwmonroe authored Aug 28, 2023
2 parents 86c22ff + b330b5b commit 2728826
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 71 deletions.
77 changes: 41 additions & 36 deletions lib/charms/layer/vsphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

# When debugging hooks, for some reason HOME is set to /home/ubuntu, whereas
# during normal hook execution, it's /root. Set it here to be consistent.
os.environ['HOME'] = '/root'
os.environ["HOME"] = "/root"


def log(msg, *args):
Expand All @@ -33,44 +33,45 @@ def save_credentials():
config = hookenv.config()

# try credentials config
if config['credentials']:
if config["credentials"]:
try:
creds_data = b64decode(config['credentials']).decode('utf8')
creds_data = b64decode(config["credentials"]).decode("utf8")
creds_data = json.loads(creds_data)
log('Using "credentials" config values for credentials')
_save_creds(creds_data)
return True
except Exception:
status.blocked('invalid value for credentials config')
status.blocked("invalid value for credentials config")
return False
no_creds_msg = 'missing credentials; set credentials config'
no_creds_msg = "missing credentials; set credentials config"

# try individual config
# NB: if a user sets one of these, they better set 'em all!
if all([config['vsphere_ip'],
config['user'],
config['password'],
config['datacenter']]):
log('Using individual config values for credentials')
if all(
[config["vsphere_ip"], config["user"], config["password"], config["datacenter"]]
):
log("Using individual config values for credentials")
_save_creds(config)
return True

# try to use Juju's trust feature
try:
result = subprocess.run(['credential-get'],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
creds_data = yaml.safe_load(result.stdout.decode('utf8'))
log('Using credentials-get for credentials')
result = subprocess.run(
["credential-get"],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
creds_data = yaml.safe_load(result.stdout.decode("utf8"))
log("Using credentials-get for credentials")
_save_creds(creds_data)
return True
except FileNotFoundError:
pass # juju trust not available
except subprocess.CalledProcessError as e:
if 'permission denied' not in e.stderr.decode('utf8'):
if "permission denied" not in e.stderr.decode("utf8"):
raise
no_creds_msg = 'missing credentials access; grant with: juju trust'
no_creds_msg = "missing credentials access; grant with: juju trust"

# no creds provided
status.blocked(no_creds_msg)
Expand All @@ -84,15 +85,15 @@ def get_vsphere_credentials():
def get_vsphere_config():
config = hookenv.config()
vsphere_config = {
'datastore': config['datastore'],
'folder': config['folder'],
'respool_path': config['respool_path'],
"datastore": config["datastore"],
"folder": config["folder"],
"respool_path": config["respool_path"],
}
# datastore and folder can't be emtpy
if not vsphere_config['datastore']:
if not vsphere_config["datastore"]:
status.blocked("Missing required 'datastore' config")
return False
if not vsphere_config['folder']:
if not vsphere_config["folder"]:
status.blocked("Missing required 'folder' config")
return False
return vsphere_config
Expand All @@ -104,25 +105,29 @@ def cleanup():

# Internal helpers


def _save_creds(creds_data):
if 'endpoint' in creds_data:
if "endpoint" in creds_data:
# we're using 'juju trust'
vsphere_ip = creds_data['endpoint']
datacenter = creds_data['region']
attrs = creds_data['credential']['attributes']
vsphere_ip = creds_data["endpoint"]
datacenter = creds_data["region"]
attrs = creds_data["credential"]["attributes"]
else:
# we're using charm config
attrs = creds_data
vsphere_ip = attrs['vsphere_ip']
datacenter = attrs['datacenter']
vsphere_ip = attrs["vsphere_ip"]
datacenter = attrs["datacenter"]

kv().set('charm.vsphere.full-creds', dict(
vsphere_ip=vsphere_ip,
user=attrs['user'],
password=attrs['password'],
datacenter=datacenter,
))
kv().set(
"charm.vsphere.full-creds",
dict(
vsphere_ip=vsphere_ip,
user=attrs["user"],
password=attrs["password"],
datacenter=datacenter,
),
)


def _load_creds():
return kv().get('charm.vsphere.full-creds')
return kv().get("charm.vsphere.full-creds")
65 changes: 32 additions & 33 deletions reactive/vsphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,67 +12,66 @@
from charms import layer


@hook('pre-series-upgrade')
@hook("pre-series-upgrade")
def pre_series_upgrade():
layer.status.blocked('Series upgrade in progress')
layer.status.blocked("Series upgrade in progress")


@when_any('config.changed.credentials',
'config.changed.vsphere_ip',
'config.changed.user',
'config.changed.password',
'config.changed.datacenter')
@when_any(
"config.changed.credentials",
"config.changed.vsphere_ip",
"config.changed.user",
"config.changed.password",
"config.changed.datacenter",
)
def update_creds():
clear_flag('charm.vsphere.creds.set')
clear_flag("charm.vsphere.creds.set")


@when_any('config.changed.datastore',
'config.changed.folder',
'config.changed.respool_path')
@when_any(
"config.changed.datastore", "config.changed.folder", "config.changed.respool_path"
)
def update_config():
clear_flag('charm.vsphere.config.set')
clear_flag("charm.vsphere.config.set")


@when_not('charm.vsphere.creds.set')
@when_not("charm.vsphere.creds.set")
def manage_creds():
toggle_flag('charm.vsphere.creds.set', layer.vsphere.save_credentials())
toggle_flag("charm.vsphere.creds.set", layer.vsphere.save_credentials())


@when_not('charm.vsphere.config.set')
@when_not("charm.vsphere.config.set")
def manage_config():
toggle_flag('charm.vsphere.config.set', layer.vsphere.get_vsphere_config())
toggle_flag("charm.vsphere.config.set", layer.vsphere.get_vsphere_config())


@when_all('charm.vsphere.creds.set',
'charm.vsphere.config.set')
@when_not('endpoint.clients.requests-pending')
@when_not('upgrade.series.in-progress')
@when_all("charm.vsphere.creds.set", "charm.vsphere.config.set")
@when_not("endpoint.clients.requests-pending")
@when_not("upgrade.series.in-progress")
def no_requests():
layer.status.active('ready')
layer.status.active("ready")


@when_all('charm.vsphere.creds.set',
'charm.vsphere.config.set',
'endpoint.clients.joined')
@when_any('config.changed',
'endpoint.clients.requests-pending')
@when_not('upgrade.series.in-progress')
@when_all(
"charm.vsphere.creds.set", "charm.vsphere.config.set", "endpoint.clients.joined"
)
@when_any("config.changed", "endpoint.clients.requests-pending")
@when_not("upgrade.series.in-progress")
def handle_requests():
clients = endpoint_from_name('clients')
config_change = is_flag_set('config.changed')
clients = endpoint_from_name("clients")
config_change = is_flag_set("config.changed")
requests = clients.all_requests if config_change else clients.new_requests
for request in requests:
layer.status.maintenance(
'granting request for {}'.format(request.unit_name))
layer.status.maintenance("granting request for {}".format(request.unit_name))
creds = layer.vsphere.get_vsphere_credentials()
config = layer.vsphere.get_vsphere_config()

request.set_credentials(**creds)
request.set_config(**config)
layer.vsphere.log('Finished request for {}', request.unit_name)
layer.vsphere.log("Finished request for {}", request.unit_name)
clients.mark_completed()


@hook('stop')
@hook("stop")
def cleanup():
layer.vsphere.cleanup()
14 changes: 12 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,18 @@ deps =
commands = pytest --tb native -s {posargs} {toxinidir}/tests/unit

[testenv:lint]
deps = flake8
commands = flake8 {toxinidir}/lib {toxinidir}/reactive {toxinidir}/tests
deps =
black
flake8
commands =
black --check {toxinidir}/lib {toxinidir}/reactive {toxinidir}/tests
flake8 {toxinidir}/lib {toxinidir}/reactive {toxinidir}/tests

[testenv:format]
deps =
black
commands =
black {toxinidir}/lib {toxinidir}/reactive {toxinidir}/tests

[testenv:integration]
deps =
Expand Down

0 comments on commit 2728826

Please sign in to comment.