--- /dev/null
+# Copyright 2021 Canonical Ltd.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+MODEL_CONFIG_KEYS = [
+ "agent-metadata-url",
+ "agent-stream",
+ "apt-ftp-proxy",
+ "apt-http-proxy",
+ "apt-https-proxy",
+ "apt-mirror",
+ "apt-no-proxy",
+ "automatically-retry-hooks",
+ "backup-dir",
+ "cloudinit-userdata",
+ "container-image-metadata-url",
+ "container-image-stream",
+ "container-inherit-properties",
+ "container-networking-method",
+ "default-series",
+ "default-space",
+ "development",
+ "disable-network-management",
+ "egress-subnets",
+ "enable-os-refresh-update",
+ "enable-os-upgrade",
+ "fan-config",
+ "firewall-mode",
+ "ftp-proxy",
+ "http-proxy",
+ "https-proxy",
+ "ignore-machine-addresses",
+ "image-metadata-url",
+ "image-stream",
+ "juju-ftp-proxy",
+ "juju-http-proxy",
+ "juju-https-proxy",
+ "juju-no-proxy",
+ "logforward-enabled",
+ "logging-config",
+ "lxd-snap-channel",
+ "max-action-results-age",
+ "max-action-results-size",
+ "max-status-history-age",
+ "max-status-history-size",
+ "net-bond-reconfigure-delay",
+ "no-proxy",
+ "provisioner-harvest-mode",
+ "proxy-ssh",
+ "snap-http-proxy",
+ "snap-https-proxy",
+ "snap-store-assertions",
+ "snap-store-proxy",
+ "snap-store-proxy-url",
+ "ssl-hostname-verification",
+ "test-mode",
+ "transmit-vendor-metrics",
+ "update-status-hook-interval",
+]
+
+
+class ModelConfig(dict):
+ prefix = "model_config_"
+
+ def __init__(self, config: dict):
+ for key, value in config.items():
+ if (
+ key.startswith(self.prefix)
+ and self._get_renamed_key(key) in MODEL_CONFIG_KEYS
+ ):
+ self.__setitem__(self._get_renamed_key(key), value)
+
+ def _get_renamed_key(self, key):
+ return key.replace(self.prefix, "").replace("_", "-")
import binascii
import base64
+from n2vc.config import ModelConfig
from n2vc.exceptions import K8sException, N2VCBadArgumentsException
from n2vc.k8s_conn import K8sConnector
from n2vc.kubectl import Kubectl, CORE_CLIENT, RBAC_CLIENT
)
port = vca_config["port"] if "port" in vca_config else 17070
url = "{}:{}".format(vca_config["host"], port)
- enable_os_upgrade = vca_config.get("enable_os_upgrade", True)
- apt_mirror = vca_config.get("apt_mirror", None)
+ model_config = ModelConfig(vca_config)
username = vca_config["user"]
secret = vca_config["secret"]
ca_cert = base64_to_cacert(vca_config["ca_cert"])
self.libjuju = Libjuju(
endpoint=url,
api_proxy=None, # Not needed for k8s charms
- enable_os_upgrade=enable_os_upgrade,
- apt_mirror=apt_mirror,
+ model_config=model_config,
username=username,
password=secret,
cacert=ca_cert,
from juju.client import client
from juju import tag
+from n2vc.config import ModelConfig
from n2vc.juju_watcher import JujuModelWatcher
from n2vc.provisioner import AsyncSSHProvisioner
from n2vc.n2vc_conn import N2VCConnector
log: logging.Logger = None,
db: dict = None,
n2vc: N2VCConnector = None,
- apt_mirror: str = None,
- enable_os_upgrade: bool = True,
+ model_config: ModelConfig = {},
):
"""
Constructor
self.n2vc = n2vc
# Generate config for models
- self.model_config = {}
- if apt_mirror:
- self.model_config["apt-mirror"] = apt_mirror
- self.model_config["enable-os-refresh-update"] = enable_os_upgrade
- self.model_config["enable-os-upgrade"] = enable_os_upgrade
+ self.model_config = model_config
self.loop.set_exception_handler(self.handle_exception)
self.creating_model = asyncio.Lock(loop=self.loop)
import logging
import os
+from n2vc.config import ModelConfig
from n2vc.exceptions import (
N2VCBadArgumentsException,
N2VCException,
)
self.api_proxy = None
- if "enable_os_upgrade" in vca_config:
- self.enable_os_upgrade = vca_config["enable_os_upgrade"]
- else:
- self.enable_os_upgrade = True
-
- if "apt_mirror" in vca_config:
- self.apt_mirror = vca_config["apt_mirror"]
- else:
- self.apt_mirror = None
+ model_config = ModelConfig(vca_config)
self.cloud = vca_config.get('cloud')
self.k8s_cloud = None
self.libjuju = Libjuju(
endpoint=self.url,
api_proxy=self.api_proxy,
- enable_os_upgrade=self.enable_os_upgrade,
- apt_mirror=self.apt_mirror,
username=self.username,
password=self.secret,
cacert=self.ca_cert,
log=self.log,
db=self.db,
n2vc=self,
+ model_config=model_config,
)
# create juju pub key file in lcm container at
mock_libjuju.assert_called_once_with(
endpoint="1.1.1.1:17070",
api_proxy=None, # Not needed for k8s charms
- enable_os_upgrade=True,
- apt_mirror=None,
+ model_config={},
username="user",
password="secret",
cacert=mock_base64_to_cacert.return_value,
log=None,
db={"get_one": []},
n2vc=n2vc,
- apt_mirror="192.168.0.100",
- enable_os_upgrade=True,
)
logging.disable(logging.CRITICAL)
loop.run_until_complete(self.libjuju.disconnect())
log=None,
db={"get_one": []},
n2vc=self.n2vc,
- apt_mirror="192.168.0.100",
- enable_os_upgrade=True,
)
mock_update_endpoints.assert_called_once_with([self.endpoint])
mock__get_api_endpoints_db.assert_called_once()
log=None,
db={"get_one": []},
n2vc=self.n2vc,
- apt_mirror="192.168.0.100",
- enable_os_upgrade=True,
)
mock_update_endpoints.assert_not_called()
mock__get_api_endpoints_db.assert_called_once()
log=None,
db={"get_one": []},
n2vc=self.n2vc,
- apt_mirror="192.168.0.100",
- enable_os_upgrade=True,
)
mock_update_endpoints.assert_called_once_with([self.endpoint])
mock__get_api_endpoints_db.assert_called_once()