diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6adf739d6c21f430de98b47bb6aa2e69dede665d..b7736f601b4bf0a3508dcd34dc3d4f074e79b1ec 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -36,10 +36,35 @@ ci_test_image: create-vps: stage: create-vps script: - - echo "hostname $HOSTNAME, subdomain $SUBDOMAIN, domain $DOMAIN, address $ADDRESS" - # Check if clusters/$HOSTNAME directory was available from cache, otherwise - # delete the old VPS and create a new VPS for this branch. - - ls clusters/$HOSTNAME/secrets || (python3 -c "import greenhost_cloud; greenhost_cloud.terminate_droplets_by_name(\"^ci-${merged_branch}\.\")"; python3 -m openappstack $HOSTNAME create --create-droplet $DOMAIN --create-hostname $HOSTNAME --ssh-key-id $SSH_KEY_ID --create-domain-records --subdomain $SUBDOMAIN) + - echo "hostname $HOSTNAME, subdomain $SUBDOMAIN, domain $DOMAIN, address $ADDRESS"; + - ls clusters/${HOSTNAME} || echo "directory clusters/${HOSTNAME} not found" + - | + # Check if cluster directory was available from cache + if [ -d clusters/$HOSTNAME/secrets ] + then + ip_address=$(python -m openappstack ${HOSTNAME} info --ip-address) + # Check if the cached machine still exists in the Greenhost cloud and it's + # the same machine. + python3 << EOF + import greenhost_cloud + machine = greenhost_cloud.get_droplets_by_name(\"^${HOSTNAME}$\")[0] + if machine['status'] == 'running' and + machine['networks']['v4'][0]['ip_address'] == '${IP_ADDRESS}': + exit(0) + else: + exit(1) + EOF + + # If exit code was 0, we can reuse the old machine, otherwise, continue to + # VPS creation + if [ $? -eq 0 ] + then + exit 0 + fi + fi + # Delete old machine if it still exists + python3 -c "import greenhost_cloud; greenhost_cloud.terminate_droplets_by_name(\"^${HOSTNAME}$\")" + python3 -m openappstack $HOSTNAME create --create-droplet $DOMAIN --create-hostname $HOSTNAME --ssh-key-id $SSH_KEY_ID --create-domain-records --subdomain $SUBDOMAIN artifacts: paths: - clusters diff --git a/openappstack/__main__.py b/openappstack/__main__.py index 385ca7e3b55eadcd7f08169ac9f06f52c2d4d291..ecd78c51600502040e6961b5f56b4759ae3b3b4a 100755 --- a/openappstack/__main__.py +++ b/openappstack/__main__.py @@ -180,6 +180,11 @@ def main(): # pylint: disable=too-many-statements,too-many-branches,too-many-lo help=("Show information about a cluster")) info_parser.set_defaults(func=info) + info_parser.add_argument( + '--ip-address', + action='store_true', + help=('Only output the IP address of the machine') + ) args = parser.parse_args() loglevel = logging.DEBUG if args.verbose else logging.INFO @@ -205,16 +210,16 @@ def main(): # pylint: disable=too-many-statements,too-many-branches,too-many-lo parser.print_help() sys.exit(1) -def info(clus, _args): +def info(clus, args): """ Shows cluster information and then exits :param cluster.Cluster clus: cluster to show information about - :param argparse.Namespace _args: ignored, added for compatibility with - create, install and test functions. + :param argparse.Namespace args: If the --ip-address argument is given, only + prints the machine's IP address. """ clus.load_data() - clus.print_info() + clus.print_info(args) def create(clus, args): # pylint: disable=too-many-branches diff --git a/openappstack/cluster.py b/openappstack/cluster.py index 15322bb12da3bb527e50ad19146b188871e68eec..21cf76e27c25b63dc54055043311b5e7b1428ee0 100644 --- a/openappstack/cluster.py +++ b/openappstack/cluster.py @@ -232,28 +232,35 @@ class Cluster: with open(config_path, 'w') as config_file: behave_config.write(config_file) - def print_info(self): - """Writes information about the cluster. Useful for debugging""" - info_string = """ -Cluster "{name}": - - IP address: {ip_address} - - Hostname: {hostname} - - Domain: {domain} - -Configuration: - - Inventory file: {inventory_file} - - Settings file: {settings_file} - -Kubectl: - -To use kubectl with this cluster, copy-paste this in your terminal: - -export KUBECONFIG={secret_dir}/kube_config_cluster.yml""" - print(info_string.format( - name=self.name, - ip_address=self.ip_address, - hostname=self.hostname, - domain=self.domain, - inventory_file=self.inventory_file, - settings_file=self.settings_file, - secret_dir=self.secret_dir)) + def print_info(self, args): + """Writes information about the cluster. Useful for debugging. + + :param argparse.Namespace args: If the --ip-address argument is given, + only prints the machine's IP address. + """ + if args.ip_address: + print(self.ip_address) + else: + info_string = """ + Cluster "{name}": + - IP address: {ip_address} + - Hostname: {hostname} + - Domain: {domain} + + Configuration: + - Inventory file: {inventory_file} + - Settings file: {settings_file} + + Kubectl: + + To use kubectl with this cluster, copy-paste this in your terminal: + + export KUBECONFIG={secret_dir}/kube_config_cluster.yml""" + print(info_string.format( + name=self.name, + ip_address=self.ip_address, + hostname=self.hostname, + domain=self.domain, + inventory_file=self.inventory_file, + settings_file=self.settings_file, + secret_dir=self.secret_dir))