-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP #1
Open
njoyce
wants to merge
1
commit into
master
Choose a base branch
from
random-refactorings
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
WIP #1
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
from charmhelpers.core import host | ||
|
||
from juju import charm, model, unit, endpoint | ||
from juju.errors import ConfigError | ||
|
||
import mariadb | ||
|
||
|
||
@model.configure | ||
def configure_workload(): | ||
""" | ||
This is called when the charm needs to be configured in some manner. | ||
This will run before any workload has been started. | ||
""" | ||
root_password = model.config.get('root-password') | ||
|
||
if not root_password: | ||
root_password = host.pwgen(32) | ||
|
||
model.config.set('root-password', root_password) | ||
|
||
|
||
@model.prepare_workload | ||
def prepare_workload(workload): | ||
""" | ||
This handler is called when the charm has been configured and is ready | ||
to deploy workloads. | ||
|
||
This should potentially work for both the machine and k8s world? | ||
Assuming that the VM is running Docker/LXD. | ||
""" | ||
root_password = model.config.get('root-password') | ||
|
||
if not root_password: | ||
raise ConfigError("Missing db root password (charm not configured?)") | ||
|
||
workload.set_oci_image(charm.resources['mariadb']) | ||
workload.open_port('db', containerPort=3306, protocol='TCP') | ||
workload.env.set('MYSQL_ROOT_PASSWORD', root_password) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this mapping to a podspec? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep, and potentially a configuration to run it in the machine world too |
||
|
||
# TODO: who/what is dealing with storage? | ||
|
||
|
||
@endpoint.join('database') | ||
def handle_join(db, request): | ||
""" | ||
Called when a request for a new database endpoint is made. | ||
|
||
If a database does not exists for this request, one will be created. | ||
|
||
@param db: The `database` endpoint. | ||
@param request: The request being made to connect to this db. | ||
""" | ||
creds = model.data.get('credentials', {}) | ||
|
||
context = creds.get(request.application_name, None) | ||
|
||
if not context: | ||
context = creds[request.application_name] = { | ||
'username': host.pwgen(20), | ||
'password': host.pwgen(20), | ||
'database': request.database_name or request.application_name | ||
} | ||
|
||
username = context['username'] | ||
password = context['password'] | ||
db_name = context['database'] | ||
|
||
with get_admin_connection() as connection: | ||
with mariadb.cursor(connection) as cursor: | ||
mariadb.create_database(cursor, db_name) | ||
mariadb.ensure_grant( | ||
cursor, | ||
db_name, | ||
username, | ||
password, | ||
# TODO: think about this from a ephemeral pod perspective | ||
request.address | ||
) | ||
|
||
request.set_state(context) | ||
request.ack() | ||
|
||
|
||
@endpoint.leave('database') | ||
def handle_leave(db_endpoint, request): | ||
""" | ||
Called when a request to leave/drop an endpoint is made. | ||
|
||
@param db: The `database` endpoint. | ||
@param request: The request being made to abandon this db. | ||
""" | ||
with get_admin_connection() as connection: | ||
with mariadb.cursor(connection) as cursor: | ||
mariadb.cleanup_grant( | ||
cursor, | ||
request.username, | ||
request.address, | ||
) | ||
|
||
request.ack() | ||
|
||
|
||
def get_admin_connection(): | ||
""" | ||
Returns a `mysql.connector.Connection` object that has 'root' privileges | ||
over the underlying mariadb. | ||
|
||
Use with care. | ||
""" | ||
root_password = model.config.get('root-password') | ||
|
||
if not root_password: | ||
raise ConfigError("Missing root password (charm not configured yet?)") | ||
|
||
return mariadb.get_connection( | ||
host=unit.host_name, | ||
password=root_password, | ||
user='root', | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you deliberately remove the charm status stuff? E.g. the existing charm code has stuff like:
IMO it's nice to be able to pipe messages out to the juju client.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea - they seemed like standard messages that the framework/library should be providing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha, yes I agree they should just have built-in ones for these hooks.