Skip to content

Commit

Permalink
[1110_uffizzi_platform] add man
Browse files Browse the repository at this point in the history
  • Loading branch information
zipofar committed Sep 5, 2023
1 parent d6201cb commit 4b21116
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 38 deletions.
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ services:
- ~/.ssh:/root/.ssh
- ~/.bash_history:/root/.bash_history
- ~/.config/uffizzi:/root/.config/uffizzi
- ~/test/uffizzi_app/charts/uffizzi-app:/gem/tmp/charts/uffizzi_app
- ~/test/uffizzi_controller_os/charts/uffizzi-controller:/gem/tmp/charts/uffizzi-controller
- bundle_cache:/bundle_cache
environment:
- BUNDLE_PATH=/bundle_cache
Expand Down
66 changes: 33 additions & 33 deletions lib/uffizzi/cli/install.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

require 'byebug'
require 'uffizzi'
require 'uffizzi/config_file'

Expand All @@ -11,7 +10,7 @@ class Cli::Install < Thor
CHART_NAME = 'uffizzi-app'
VALUES_FILE_NAME = 'helm_values.yaml'
DEFAULT_ISSUER = 'letsencrypt'
DEFAULT_NAMESPACE = 'default'
DEFAULT_NAMESPACE = 'uffizzi'
DEFAULT_APP_PREFIX = 'uffizzi'

desc 'application', 'Install uffizzi to cluster'
Expand All @@ -20,7 +19,6 @@ class Cli::Install < Thor
method_option :'user-email', type: :string
method_option :'acme-email', type: :string
method_option :'user-password', type: :string
method_option :'controller-password', type: :string
method_option :issuer, type: :string, enum: ['letsencrypt', 'zerossl']
method_option :'wildcard-cert-path', type: :string
method_option :'wildcard-key-path', type: :string
Expand All @@ -42,10 +40,11 @@ def application
method_option :cert, type: :string
method_option :key, type: :string
method_option :namespace, type: :string
method_option :repo, type: :string
def wildcard_tls
kubectl_exists?

params = if options.present? && wildcard_tls_options_valid?
params = if options.except(:repo).present? && wildcard_tls_options_valid?
{
namespace: options[:namespace] || DEFAULT_NAMESPACE,
domain: options[:domain],
Expand All @@ -55,18 +54,25 @@ def wildcard_tls
else
namespace = Uffizzi.prompt.ask('Namespace: ', required: true, default: DEFAULT_NAMESPACE)
domain = Uffizzi.prompt.ask('Domain: ', required: true, default: 'example.com')
wildcard_cert_paths = ask_wildcard_cert(has_user_wildcard_cert: true)
wildcard_cert_paths = ask_wildcard_cert(has_user_wildcard_cert: true, domain: domain)

{ namespace: namespace, domain: domain }.merge(wildcard_cert_paths)
end

kubectl_add_wildcard_tls(params)
helm_values = helm_get_values(namespace, namespace)
helm_values['uffizzi-controller']['tlsPerDeploymentEnabled'] = false.to_s
create_helm_values_file(helm_values)
helm_set_repo unless options[:repo]
helm_install(release_name: namespace, namespace: namespace, repo: options[:repo])
end

default_task :application

private

def wildcard_tls_options_valid?
required_options = [:domain, :cert, :key]
required_options = [:namespace, :domain, :cert, :key]
missing_options = required_options - options.symbolize_keys.keys

return true if missing_options.empty?
Expand All @@ -85,9 +91,12 @@ def run_installation

create_helm_values_file(helm_values)
helm_set_repo unless options[:repo]
helm_set_release(params.fetch(:namespace))
helm_install(release_name: params[:namespace], namespace: params[:namespace], repo: options[:repo])
kubectl_add_wildcard_tls(params) if params[:wildcard_cert_path] && params[:wildcard_key_path]
delete_helm_values_file

Uffizzi.ui.say('Helm release is deployed')
Uffizzi.ui.say("The uffizzi application url is https://#{DEFAULT_APP_PREFIX}.#{params[:domain]}")
end

def kubectl_exists?
Expand All @@ -107,16 +116,6 @@ def helm_set_repo
helm_repo_add
end

def helm_set_release(namespace)
releases = helm_release_list(namespace)
release = releases.detect { |r| r['name'] == namespace }
if release.present?
Uffizzi.ui.say_error_and_exit("The release #{release['name']} already exists with status #{release['status']}")
end

helm_install(namespace)
end

def helm_repo_add
cmd = "helm repo add #{HELM_REPO_NAME} https://uffizzicloud.github.io/uffizzi"
execute_command(cmd)
Expand All @@ -130,32 +129,31 @@ def helm_repo_search
end
end

def helm_release_list(namespace)
cmd = "helm list -n #{namespace} -o json"
result = execute_command(cmd, say: false)

JSON.parse(result)
end

def helm_install(namespace)
def helm_install(release_name:, namespace:, repo:)
Uffizzi.ui.say('Start helm release installation')

release_name = namespace
repo = options[:repo] || "#{HELM_REPO_NAME}/#{CHART_NAME}"
cmd = "helm install #{release_name} #{repo}" \
repo = repo || "#{HELM_REPO_NAME}/#{CHART_NAME}"
cmd = "helm upgrade #{release_name} #{repo}" \
" --values #{helm_values_file_path}" \
" --namespace #{namespace}" \
' --create-namespace' \
' --install' \
' --output json'

res = execute_command(cmd, say: false)
info = JSON.parse(res)['info']

return Uffizzi.ui.say('Helm release is deployed') if info['status'] == HELM_DEPLOYED_STATUS
return if info['status'] == HELM_DEPLOYED_STATUS

Uffizzi.ui.say_error_and_exit(info)
end

def helm_get_values(release_name, namespace)
cmd = "helm get values #{release_name} -n #{namespace} -o json"
res = execute_command(cmd, say: false)
JSON.parse(res)
end

def kubectl_add_wildcard_tls(params)
cmd = "kubectl create secret tls wildcard.#{params.fetch(:domain)}" \
" --cert=#{params.fetch(:wildcard_cert_path)}" \
Expand All @@ -165,7 +163,7 @@ def kubectl_add_wildcard_tls(params)
execute_command(cmd)
end

def ask_wildcard_cert(has_user_wildcard_cert: nil)
def ask_wildcard_cert(has_user_wildcard_cert: nil, domain: nil)
has_user_wildcard_cert ||= Uffizzi.prompt.yes?('Uffizzi use a wildcard tls certificate. Do you have it?')

if has_user_wildcard_cert
Expand All @@ -177,13 +175,12 @@ def ask_wildcard_cert(has_user_wildcard_cert: nil)

Uffizzi.ui.say('Uffizzi does not work properly without a wildcard certificate.')
Uffizzi.ui.say('You can add wildcard cert later with command:')
Uffizzi.ui.say('uffizzi install wildcard-tls --domain your.domain.com --cert /path/to/cert --key /path/to/key')
Uffizzi.ui.say("uffizzi install wildcard-tls --domain #{domain} --cert /path/to/cert --key /path/to/key")

{}
end

def ask_installation_params
wildcard_cert_paths = ask_wildcard_cert
namespace = Uffizzi.prompt.ask('Namespace: ', required: true, default: DEFAULT_NAMESPACE)
domain = Uffizzi.prompt.ask('Domain: ', required: true, default: 'example.com')
user_email = Uffizzi.prompt.ask('User email: ', required: true, default: "admin@#{domain}")
Expand All @@ -194,6 +191,7 @@ def ask_installation_params
{ name: 'ZeroSSL', value: 'zerossl' },
]
cluster_issuer = Uffizzi.prompt.select('Cluster issuer', cluster_issuers)
wildcard_cert_paths = ask_wildcard_cert(domain: domain)

{
namespace: namespace,
Expand Down Expand Up @@ -226,7 +224,7 @@ def build_installation_options
domain: options[:domain],
user_email: options[:'user-email'] || "admin@#{options[:domain]}",
user_password: options[:'user-password'] || generate_password,
controller_password: options[:'controller-password'] || generate_password,
controller_password: generate_password,
cert_email: options[:'acme-email'] || options[:'user-email'],
cluster_issuer: options[:issuer] || DEFAULT_ISSUER,
wildcard_cert_path: options[:'wildcard-cert-path'],
Expand All @@ -237,6 +235,7 @@ def build_installation_options
def build_helm_values(params)
domain = params.fetch(:domain)
namespace = params.fetch(:namespace)
tls_per_deployment_enabled = params.slice(:wildcard_cert_pathm, :wildcard_key_path).compact.empty?
app_host = [DEFAULT_APP_PREFIX, domain].join('.')

{
Expand All @@ -260,6 +259,7 @@ def build_helm_values(params)
disabled: true,
},
clusterIssuer: params.fetch(:cluster_issuer),
tlsPerDeploymentEnabled: tls_per_deployment_enabled.to_s,
certEmail: params.fetch(:cert_email),
'ingress-nginx' => {
controller: {
Expand Down
76 changes: 76 additions & 0 deletions man/uffizzi-install
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
.\" generated with Ronn-NG/v0.9.1
.\" http://github.com/apjanke/ronn-ng/tree/0.9.1
.TH "INSTALL" "" "September 2023" ""
.SH "NAME"
\fBinstall\fR \- install the Uffizzi application to cluster
.SH "SYNOPSIS"
.nf
uffizzi install COMMAND
.fi
.SH "DESCRIPTION"
.nf
The uffizzi install command lets you deploy uffizzi application to your kubecrnetes cluster\.
If COMMAND is not specified, uffizzi install start installation\.
if OPTIONS not specified, uffizzi show installation wizard\.

For more information on configuration options, see:
https://docs\.uffizzi\.com/references/cli/
.fi
.SH "COMMANDS"
.nf
COMMAND is one of the following:

wildcard_tls OPTION
Add the wildcard tls certificate to installed uffizzi application\.
.fi
.SH "OPTIONS"
.nf
OPTION is one of the following:

namespace
The namespace of the kubecrnetes cluster where application will be deployed\.
Default is uffizzi\.

domain
The domain that will be used for access the web API\.

issuer
The cluster issuer that will be used for generate tls certificates\.
Default is letsencrypt\.

user\-email
The login that will be used for access to web API\.

user\-password
The password that will be used for access to web API\.

acme\-email
Email address for ACME registration

wildcard\-cert\-path
Path to wildcard certificate\.

wildcard\-key\-path
Path to wildcard certificate key\.

without\-wildcard\-tls
Set this flag and we can install application without wildcard certificate\.

print\-values
Show builded vales for helm installation\.
The installation will not be executed\.

repo
The repository that will be used for helm install
.fi
.SH "EXAMPLES"
.nf
To install the uffizzi command, run:

$ uffizzi install

To install the wildcard_tls command, run:

$ uffizzi install wildcard_tls
.fi

67 changes: 67 additions & 0 deletions man/uffizzi-install.ronn
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
uffizzi install - install the Uffizzi application to cluster
================================================================

## SYNOPSIS
uffizzi install COMMAND

## DESCRIPTION
The uffizzi install command lets you deploy uffizzi application to your kubecrnetes cluster.
If COMMAND is not specified, uffizzi install start installation.
if OPTIONS not specified, uffizzi show installation wizard.

For more information on configuration options, see:
https://docs.uffizzi.com/references/cli/

## COMMANDS
COMMAND is one of the following:

wildcard_tls OPTION
Add the wildcard tls certificate to installed uffizzi application.

## OPTIONS
OPTION is one of the following:

namespace
The namespace of the kubecrnetes cluster where application will be deployed.
Default is uffizzi.

domain
The domain that will be used for access the web API.

issuer
The cluster issuer that will be used for generate tls certificates.
Default is letsencrypt.

user-email
The login that will be used for access to web API.

user-password
The password that will be used for access to web API.

acme-email
Email address for ACME registration

wildcard-cert-path
Path to wildcard certificate.

wildcard-key-path
Path to wildcard certificate key.

without-wildcard-tls
Set this flag and we can install application without wildcard certificate.

print-values
Show builded vales for helm installation.
The installation will not be executed.

repo
The repository that will be used for helm install

## EXAMPLES
To install the uffizzi command, run:

$ uffizzi install

To install the wildcard_tls command, run:

$ uffizzi install wildcard_tls
9 changes: 4 additions & 5 deletions test/uffizzi/cli/install_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,25 @@ def test_install_by_wizard
@mock_shell.promise_execute(/helm search repo/, stdout: [].to_json)
@mock_shell.promise_execute(/helm repo add/, stdout: 'ok')
@mock_shell.promise_execute(/helm list/, stdout: [].to_json)
@mock_shell.promise_execute(/helm install/, stdout: { info: { status: 'deployed' } }.to_json)
@mock_shell.promise_execute(/helm upgrade/, stdout: { info: { status: 'deployed' } }.to_json)

@install.application

last_message = Uffizzi.ui.last_message
assert_match('deployed', last_message)
assert_match('The uffizzi application url is', last_message)
end

def test_install_by_options
@mock_shell.promise_execute(/kubectl version/, stdout: '1.23.00')
@mock_shell.promise_execute(/helm version/, stdout: '3.00')
@mock_shell.promise_execute(/helm search repo/, stdout: [].to_json)
@mock_shell.promise_execute(/helm repo add/, stdout: 'ok')
@mock_shell.promise_execute(/helm list/, stdout: [].to_json)
@mock_shell.promise_execute(/helm install/, stdout: { info: { status: 'deployed' } }.to_json)
@mock_shell.promise_execute(/helm upgrade/, stdout: { info: { status: 'deployed' } }.to_json)

@install.options = command_options(domain: 'my-domain.com', 'without-wildcard-tls' => true)
@install.application

last_message = Uffizzi.ui.last_message
assert_match('deployed', last_message)
assert_match('The uffizzi application url is', last_message)
end
end

0 comments on commit 4b21116

Please sign in to comment.