Skip to content

Commit

Permalink
Merge pull request #5 from huntdatacenter/dev
Browse files Browse the repository at this point in the history
updates
  • Loading branch information
matuskosut authored Jul 30, 2024
2 parents c2cfe27 + b1f6fa0 commit 563d6cb
Show file tree
Hide file tree
Showing 20 changed files with 682 additions and 123 deletions.
26 changes: 20 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Use one shell for all commands in a target recipe
.ONESHELL:
.EXPORT_ALL_VARIABLES:
.PHONY: help clean rename build deploy list launch mount umount bootstrap up down ssh destroy bridge
# Set default goal
.DEFAULT_GOAL := help
Expand All @@ -10,9 +11,11 @@ SHELL := /bin/bash

# Charm variables
CHARM_NAME := ansible.charm
CHARMHUB_NAME := huntdatacenter-ansible
CHARM_STORE_URL := https://charmhub.io/huntdatacenter-charm-ansible
CHARM_HOMEPAGE := https://github.com/huntdatacenter/charm-ansible/
CHARM_BUGS_URL := https://github.com/huntdatacenter/charm-ansible/issues
CHARM_BUILD := ansible_ubuntu-20.04-amd64-arm64_ubuntu-22.04-amd64-arm64_ubuntu-24.04-amd64-arm64.charm

# Multipass variables
UBUNTU_VERSION = jammy
Expand All @@ -22,19 +25,29 @@ VM_NAME = juju-dev--$(DIR_NAME)

clean: ## Remove artifacts
charmcraft clean --verbose
rm -vf ansible_ubuntu-20.04-amd64-arm64_ubuntu-22.04-amd64-arm64.charm
rm -vf $(CHARM_BUILD) $(CHARM_NAME)

ansible_ubuntu-20.04-amd64-arm64_ubuntu-22.04-amd64-arm64.charm:
$(CHARM_BUILD):
charmcraft pack --verbose

rename:
mv -v ansible_ubuntu-20.04-amd64-arm64_ubuntu-22.04-amd64-arm64.charm $(CHARM_NAME)
$(CHARM_NAME): $(CHARM_BUILD)
mv -v $(CHARM_BUILD) $(CHARM_NAME)

build: ansible_ubuntu-20.04-amd64-arm64_ubuntu-22.04-amd64-arm64.charm rename ## Build charm
build: $(CHARM_NAME) ## Build charm

clean-build: $(CHARM_NAME) ## Build charm from scratch

deploy: ## Deploy charm
juju deploy ./$(CHARM_NAME)

login:
bash -c "test -s ~/.charmcraft-auth || charmcraft login --export ~/.charmcraft-auth"

release: login ## Release charm
@echo "# -- Releasing charm: https://charmhub.io/$(CHARMHUB_NAME)"
$(eval CHARMCRAFT_AUTH := $(shell cat ~/.charmcraft-auth))
charmcraft upload --name $(CHARMHUB_NAME) --release latest/stable $(CHARM_NAME)


name: ## Print name of the VM
echo "$(VM_NAME)"
Expand All @@ -56,7 +69,8 @@ umount:
bootstrap:
$(eval ARCH := $(shell multipass exec $(VM_NAME) -- dpkg --print-architecture))
multipass exec $(VM_NAME) -- juju bootstrap localhost lxd --bootstrap-constraints arch=$(ARCH) \
&& multipass exec $(VM_NAME) -- juju add-model default
&& multipass exec $(VM_NAME) -- juju add-model default \
&& multipass exec $(VM_NAME) -- juju model-config enable-os-upgrade=false

up: launch mount bootstrap ssh ## Start a VM

Expand Down
21 changes: 20 additions & 1 deletion actions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,31 @@

ansible-playbook:
description: Run ansible playbook
parallel: false
params:
tags:
description: "Execute Tags"
description: "Comma separate string of tags. Only run plays and tasks tagged with these values."
type: string
extra:
description: "Json encoded string with extra variables for playbook"
type: string
diff:
description: "Log ansible diff (enable with: diff=1)"
type: integer
minimum: 0
maximum: 1
check:
description: "Use ansible check mode - dry run (enable with: check=1)"
type: integer
minimum: 0
maximum: 1
verbosity:
description: |
Set ansible logging verbosity from 1 to 6 (default: 0)
Setting verbosity=4 is equivalent of adding -vvvv after ansible-playbook command.
type: integer
minimum: 0
maximum: 6
required:
- tags
1 change: 1 addition & 0 deletions ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[defaults]
1 change: 1 addition & 0 deletions bundles/crontab.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* * * * * root /usr/bin/true
19 changes: 19 additions & 0 deletions bundles/noble.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
machines:
'0':
series: noble
relations:
- - ubuntu:juju-info
- cmatrix:juju-info
series: noble
applications:
ubuntu:
charm: huntdatacenter-ubuntu-lite
num_units: 1
series: noble
to:
- '0'
cmatrix:
charm: ../ansible.charm
series: noble
options:
playbook: "include-file://playbook.yaml"
72 changes: 72 additions & 0 deletions bundles/storage/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Using Ansible charm to add storage

Find existing storage pool:
```bash
juju list-storage-pools
```

Or create a new one:
```bash
juju help create-storage-pool
```

```bash
juju deploy ./bundles/storage/bundle.yaml
```

Upgrade of charm:
```bash
juju refresh ubuntu-storage --path ./ansible.charm
```

Running action to print debug logs from inside ansible:
```bash
juju run ubuntu-storage/leader ansible-playbook diff=1 check=1 tags=debug verbosity=4
```

```
TASK [Print all extra vars] ****************************************************
ok: [localhost] => {
"hostvars[inventory_hostname]": {
"ansible_check_mode": true,
"ansible_config_file": null,
"ansible_connection": "local",
"ansible_facts": {},
"ansible_playbook_python": "/usr/bin/python3",
"ansible_remote_tmp": "/root/.ansible/tmp",
"ansible_version": "Unknown",
"app_name": "ubuntu-storage",
"charm_dir": "/var/lib/juju/agents/unit-ubuntu-storage-0/charm",
"crontab": "",
"group_names": [
"ungrouped"
],
"groups": {
"all": [
"localhost"
],
"ungrouped": [
"localhost"
]
},
"ingress_address": "10.209.9.135",
"inventory_dir": "/etc/ansible",
"inventory_file": "/etc/ansible/hosts",
"inventory_hostname": "localhost",
"inventory_hostname_short": "localhost",
"leader": true,
"local_unit": "ubuntu-storage/0",
"omit": "__omit_place_holder__5f7ea0c7965826acfa7911dcbc4ca9982b7339d4",
"playbook": "- hosts: localhost\n connection: local\n become: true\n gather_facts: false\n\n tasks:\n - name: Run apt update\n ansible.builtin.apt:\n update_cache: true\n become: true\n tags:\n - never\n - install\n - start\n\n - name: Print all available facts\n ansible.builtin.debug:\n var: ansible_facts\n ignore_errors: true\n tags:\n - never\n - install\n - debug\n\n - name: Print all extra vars\n ansible.builtin.debug:\n var: hostvars[inventory_hostname]\n ignore_errors: true\n tags:\n - never\n - install\n - debug\n\n # - name: Install cmatrix\n # ansible.builtin.apt:\n # name:\n # - \"cmatrix\"\n # become: true\n # tags:\n # - never\n # - install\n # - start\n\n # - name: Remove cmatrix\n # ansible.builtin.apt:\n # name:\n # - \"cmatrix\"\n # state: absent\n # become: true\n # tags:\n # - never\n # - stop\n",
"playbook_dir": "/var/lib/juju/agents/unit-ubuntu-storage-0/charm",
"storage_bind_mount": "/opt/charm-ansible/ubuntu-storage/storage",
"storage_mount": "",
"storage_volume": "/var/lib/juju/storage/data/1",
"storages": {
"data": "/var/lib/juju/storage/data/1"
},
"unit_private_address": "10.209.9.135",
"unit_public_address": "10.209.9.135"
}
}
```
34 changes: 34 additions & 0 deletions bundles/storage/bundle.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
machines:
'0':
base: ubuntu@24.04
# series: noble

default-base: ubuntu@24.04
# series: noble

relations:
- - ubuntu:juju-info
- ubuntu-storage:juju-info

applications:
ubuntu:
charm: huntdatacenter-ubuntu-lite
num_units: 1
to:
- '0'
ubuntu-storage:
charm: ../../ansible.charm
# juju config ubuntu-storage playbook=@playbook.yaml
options:
playbook: "include-file://playbook.yaml"
# /etc/cron.d/charm_ubuntu_storage
crontab: |
* * * * * root /usr/bin/true
# -- List storage pools to find out whether you will use lxd, cinder, or something else
#
# juju list-storage-pools
# juju help create-storage-pool
storage:
data: lxd,2G
# data: cinder,100G
# -- Mount path will be something like: /var/lib/juju/storage/data/0
30 changes: 30 additions & 0 deletions bundles/storage/playbook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
- hosts: localhost
connection: local
become: true
gather_facts: false

tasks:
- name: Run apt update
ansible.builtin.apt:
update_cache: true
become: true
tags:
- never
- install
- start

- name: Print all environment variables
ansible.builtin.debug:
var: environment
ignore_errors: true
tags:
- never
- debug

- name: Print all extra vars
ansible.builtin.debug:
var: hostvars[inventory_hostname]
ignore_errors: true
tags:
- never
- debug
10 changes: 9 additions & 1 deletion charmcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ parts:
- pkg-config
charm-python-packages:
- setuptools
prime:
- ansible.cfg
- playbooks/storage.yaml
bases:
- build-on:
- name: "ubuntu"
channel: "20.04"
channel: "22.04"
run-on:
- name: "ubuntu"
channel: "20.04"
Expand All @@ -27,3 +30,8 @@ bases:
architectures:
- amd64
- arm64
- name: "ubuntu"
channel: "24.04"
architectures:
- amd64
- arm64
27 changes: 25 additions & 2 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,35 @@

options:
playbook:
default: "{}"
default: |
- hosts: localhost
connection: local
become: true
gather_facts: false
tasks: []
type: string
description: |
Ansible playbook file.
Use yaml file with keys environment and hosts.
Bundle example: 'include-file://files/playbook.yaml'
Command example: juju config appname playbook=@playbook.yaml
Command example: 'juju config app_name playbook=@playbook.yaml'
storage_mount:
default: ""
type: string
description: |
If storage is added, it will be bind mounted on this path.
Default: /opt/charm-ansible/<app_name>/storage
crontab:
default: ""
type: string
description: |
Crontab config for running regular tasks. Use multiple lines to add more cron jobs.
Cron jobs will be placed in /etc/cron.d/charm_<app_name> config file.
https://help.ubuntu.com/community/CronHowto#:~:text=Two%20Other%20Types%20of%20Crontab,-The%20crontab
Formatting: '<minute> <hour> <day> <month> <weekday> <username> <script-with-parameters>'
Example: '0 0 * * * root /usr/bin/true'
Configure from example file: 'juju config $app_name playbook=@bundles/crontab.txt'
3 changes: 1 addition & 2 deletions juju.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ snap:
commands:
- snap install multipass-sshfs
# - snap install lxd --channel=5.0/stable
- snap install juju --channel=3.1/stable
- snap install juju --channel=3.5/stable
- snap install charmcraft --classic --channel=2.x/stable
# - snap install charm --classic --channel=stable
# - snap refresh
Expand Down Expand Up @@ -66,4 +66,3 @@ runcmd:
ln -sf /home/ubuntu/vagrant /vagrant
final_message: "The system is finally up, after $UPTIME seconds"

2 changes: 1 addition & 1 deletion lib/extensions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .ansible_playbook import Ansible

ansible = Ansible()
ansible_manager = Ansible()
Loading

0 comments on commit 563d6cb

Please sign in to comment.