Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
moklidia committed Oct 16, 2023
2 parents 712afee + 5019108 commit 7b2cadc
Show file tree
Hide file tree
Showing 20 changed files with 72 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
uffizzi-cli (2.1.2)
uffizzi-cli (2.1.3)
activesupport
awesome_print
faker
Expand Down
20 changes: 16 additions & 4 deletions lib/uffizzi/auth_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Uffizzi
module AuthHelper
class << self
def signed_in?
config_data_exists? || Uffizzi::Token.exists?
config_data_exists? && authorized?
end

def sign_out
Expand All @@ -16,13 +16,25 @@ def sign_out
Uffizzi::Token.delete if Uffizzi::Token.exists?
end

def check_login(project_option)
raise Uffizzi::Error.new('You are not logged in. Run `uffizzi login`.') unless signed_in?
raise Uffizzi::Error.new('This command needs project to be set in config file') unless project_set?(project_option)
end

private

def config_data_exists?
ConfigFile.exists? &&
ConfigFile.option_has_value?(:account) &&
ConfigFile.option_has_value?(:cookie) &&
ConfigFile.option_has_value?(:server)
ConfigFile.option_has_value?(:server) &&
ConfigFile.option_has_value?(:account)
end

def authorized?
ConfigFile.option_has_value?(:cookie) || Uffizzi::Token.exists?
end

def project_set?(project_option)
!project_option.nil? || (Uffizzi::ConfigFile.exists? && Uffizzi::ConfigFile.option_has_value?(:project))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/uffizzi/cli/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def set_default(account_name)
private

def run(command, account_name = nil)
return Uffizzi.ui.say('You are not logged in.') unless Uffizzi::AuthHelper.signed_in?
Uffizzi::AuthHelper.check_login(options[:project])

case command
when 'list'
Expand Down
29 changes: 16 additions & 13 deletions lib/uffizzi/cli/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require 'uffizzi/auth_helper'
require 'uffizzi/helpers/config_helper'
require 'uffizzi/services/preview_service'
require 'uffizzi/services/command_service'
require 'uffizzi/services/cluster_service'
require 'uffizzi/services/kubeconfig_service'
require 'uffizzi/services/cluster/disconnect_service'
Expand All @@ -30,6 +29,7 @@ def list
method_option :'update-current-context', type: :boolean, required: false, default: true
method_option :output, required: false, type: :string, aliases: '-o', enum: ['json', 'pretty-json']
method_option :'creation-source', required: false, type: :string
method_option :'k8s-version', required: false, type: :string
def create(name = nil)
run('create', { name: name })
end
Expand Down Expand Up @@ -65,9 +65,7 @@ def disconnect

def run(command, command_args = {})
Uffizzi.ui.output_format = options[:output]
raise Uffizzi::Error.new('You are not logged in.') unless Uffizzi::AuthHelper.signed_in?
raise Uffizzi::Error.new('This command needs project to be set in config file') unless CommandService.project_set?(options)

Uffizzi::AuthHelper.check_login(options[:project])
project_slug = options[:project].nil? ? ConfigFile.read_option(:project) : options[:project]

case command
Expand Down Expand Up @@ -114,13 +112,15 @@ def handle_create_command(project_slug, command_args)

cluster_name = command_args[:name] || options[:name] || ClusterService.generate_name
creation_source = options[:"creation-source"] || ClusterService::MANUAL_CREATION_SOURCE

unless ClusterService.valid_name?(cluster_name)
Uffizzi.ui.say_error_and_exit("Cluster name: #{cluster_name} is not valid.")
end

manifest_file_path = options[:manifest]
params = cluster_creation_params(cluster_name, creation_source, manifest_file_path)
k8s_version = options[:"k8s-version"]
Uffizzi.ui.say_error_and_exit("Cluster name: #{cluster_name} is not valid.") unless ClusterService.valid_name?(cluster_name)

params = cluster_creation_params(
name: cluster_name,
creation_source: creation_source,
manifest_file_path: options[:manifest],
k8s_version: k8s_version,
)
response = create_cluster(ConfigFile.read_option(:server), project_slug, params)

return ResponseHelper.handle_failed_response(response) unless ResponseHelper.created?(response)
Expand Down Expand Up @@ -239,7 +239,7 @@ def say_error_update_kubeconfig(cluster_data)
end
end

def cluster_creation_params(name, creation_source, manifest_file_path)
def cluster_creation_params(name:, creation_source:, manifest_file_path:, k8s_version:)
manifest_content = load_manifest_file(manifest_file_path)
oidc_token = Uffizzi::ConfigFile.read_option(:oidc_token)

Expand All @@ -248,6 +248,7 @@ def cluster_creation_params(name, creation_source, manifest_file_path)
name: name,
manifest: manifest_content,
creation_source: creation_source,
k8s_version: k8s_version,
},
token: oidc_token,
}
Expand Down Expand Up @@ -303,6 +304,7 @@ def handle_succeed_describe(cluster_data)
status: cluster_data[:state],
created: Time.strptime(cluster_data[:created_at], '%Y-%m-%dT%H:%M:%S.%N').strftime('%a %b %d %H:%M:%S %Y'),
url: cluster_data[:host],
k8s_version: cluster_data[:k8s_version],
}

rendered_cluster_data = if Uffizzi.ui.output_format.nil?
Expand Down Expand Up @@ -335,7 +337,6 @@ def handle_succeed_create_response(cluster_data)
end

def save_kubeconfig(kubeconfig, kubeconfig_path)
kubeconfig_path = kubeconfig_path.nil? ? KubeconfigService.default_path : kubeconfig_path
is_update_current_context = options[:'update-current-context']

KubeconfigService.save_to_filepath(kubeconfig_path, kubeconfig) do |kubeconfig_by_path|
Expand All @@ -354,6 +355,8 @@ def save_kubeconfig(kubeconfig, kubeconfig_path)
merged_kubeconfig
end
end

Uffizzi.ui.say("Kubeconfig was updated by the path: #{kubeconfig_path}") if is_update_current_context
end

def update_clusters_config(id, params)
Expand Down
21 changes: 10 additions & 11 deletions lib/uffizzi/cli/dev.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

require 'uffizzi/services/command_service'
require 'uffizzi/services/cluster_service'
require 'uffizzi/services/dev_service'
require 'uffizzi/services/kubeconfig_service'
require 'uffizzi/auth_helper'

module Uffizzi
class Cli::Dev < Thor
Expand All @@ -13,11 +13,12 @@ class Cli::Dev < Thor
method_option :quiet, type: :boolean, aliases: :q
method_option :'default-repo', type: :string
method_option :kubeconfig, type: :string
method_option :'k8s-version', required: false, type: :string
def start(config_path = 'skaffold.yaml')
Uffizzi::AuthHelper.check_login(options[:project])
DevService.check_skaffold_existence
DevService.check_running_daemon if options[:quiet]
DevService.check_skaffold_config_existence(config_path)
check_login
cluster_id, cluster_name = start_create_cluster
kubeconfig = wait_cluster_creation(cluster_name)

Expand Down Expand Up @@ -49,15 +50,12 @@ def stop

private

def check_login
raise Uffizzi::Error.new('You are not logged in.') unless Uffizzi::AuthHelper.signed_in?
raise Uffizzi::Error.new('This command needs project to be set in config file') unless CommandService.project_set?(options)
end

def start_create_cluster
cluster_name = ClusterService.generate_name
creation_source = ClusterService::MANUAL_CREATION_SOURCE
params = cluster_creation_params(cluster_name, creation_source)
params = cluster_creation_params(
name: ClusterService.generate_name,
creation_source: ClusterService::MANUAL_CREATION_SOURCE,
k8s_version: options[:"k8s-version"],
)
Uffizzi.ui.say('Start creating a cluster')
response = create_cluster(ConfigFile.read_option(:server), project_slug, params)
return ResponseHelper.handle_failed_response(response) unless ResponseHelper.created?(response)
Expand Down Expand Up @@ -110,14 +108,15 @@ def update_clusters_config(id, params)
ConfigFile.write_option(:clusters, clusters_config)
end

def cluster_creation_params(name, creation_source)
def cluster_creation_params(name:, creation_source:, k8s_version:)
oidc_token = Uffizzi::ConfigFile.read_option(:oidc_token)

{
cluster: {
name: name,
manifest: nil,
creation_source: creation_source,
k8s_version: k8s_version,
},
token: oidc_token,
}
Expand Down
4 changes: 1 addition & 3 deletions lib/uffizzi/cli/preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
require 'uffizzi'
require 'uffizzi/auth_helper'
require 'uffizzi/services/preview_service'
require 'uffizzi/services/command_service'
require 'uffizzi/services/github_service'

module Uffizzi
Expand Down Expand Up @@ -55,8 +54,7 @@ def events(deployment_name)

def run(command, file_path: nil, deployment_name: nil)
Uffizzi.ui.output_format = options[:output]
raise Uffizzi::Error.new('You are not logged in.') unless Uffizzi::AuthHelper.signed_in?
raise Uffizzi::Error.new('This command needs project to be set in config file') unless CommandService.project_set?(options)
Uffizzi::AuthHelper.check_login(options[:project])

project_slug = options[:project].nil? ? ConfigFile.read_option(:project) : options[:project]

Expand Down
7 changes: 2 additions & 5 deletions lib/uffizzi/cli/preview/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
require 'uffizzi/auth_helper'
require 'uffizzi/response_helper'
require 'uffizzi/services/preview_service'
require 'uffizzi/services/command_service'

module Uffizzi
class Cli::Preview::Service < Thor
include ApiClient

desc 'logs [LOGS_TYPE] [DEPLOYMENT_ID] [CONTAINER_NAME]', 'Show the logs for a container service of a preview'
def logs(logs_type, deployment_name, container_name = args)
return Uffizzi.ui.say('You are not logged in.') unless Uffizzi::AuthHelper.signed_in?
return Uffizzi.ui.say('This command needs project to be set in config file') unless CommandService.project_set?(options)
Uffizzi::AuthHelper.check_login(options[:project])

deployment_id = PreviewService.read_deployment_id(deployment_name)
response = service_logs_response(logs_type, deployment_id, container_name)
Expand All @@ -28,8 +26,7 @@ def logs(logs_type, deployment_name, container_name = args)

desc 'list [DEPLOYMENT_ID]', 'List the container services of a given compose environment (preview)'
def list(deployment_name)
return Uffizzi.ui.say('You are not logged in.') unless Uffizzi::AuthHelper.signed_in?
return Uffizzi.ui.say('This command needs project to be set in config file') unless CommandService.project_set?(options)
Uffizzi::AuthHelper.check_login(options[:project])

project_slug = options[:project].nil? ? ConfigFile.read_option(:project) : options[:project]
server = ConfigFile.read_option(:server)
Expand Down
2 changes: 1 addition & 1 deletion lib/uffizzi/cli/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def delete(project_slug)
private

def run(command, project_slug: nil)
return Uffizzi.ui.say('You are not logged in.') unless Uffizzi::AuthHelper.signed_in?
Uffizzi::AuthHelper.check_login(options[:project])

case command
when 'list'
Expand Down
4 changes: 1 addition & 3 deletions lib/uffizzi/cli/project/compose.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
require 'uffizzi/response_helper'
require 'uffizzi/services/compose_file_service'
require 'uffizzi/services/env_variables_service'
require 'uffizzi/services/command_service'

module Uffizzi
class Cli::Project::Compose < Thor
Expand All @@ -29,8 +28,7 @@ def describe
private

def run(command)
return Uffizzi.ui.say('You are not logged in.') unless Uffizzi::AuthHelper.signed_in?
return Uffizzi.ui.say('This command needs project to be set in config file') unless CommandService.project_set?(options)
Uffizzi::AuthHelper.check_login(options[:project])

project_slug = options[:project].nil? ? ConfigFile.read_option(:project) : options[:project]
file_path = options[:file]
Expand Down
9 changes: 0 additions & 9 deletions lib/uffizzi/services/command_service.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/uffizzi/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Uffizzi
VERSION = '2.1.2'
VERSION = '2.1.3'
end
11 changes: 10 additions & 1 deletion man/uffizzi-cluster-create.ronn
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
uffizzi cluster create -h
uffizzi-cluster-create - create a cluster
================================================================

Expand All @@ -12,9 +13,17 @@ uffizzi-cluster-create - create a cluster
https://docs.uffizzi.com/references/cli/

## FLAGS

--name
Option is deprecated and will be removed in the newer versions.
Please use a positional argument instead: uffizzi cluster create my-awesome-name.
Please use a positional argument instead: uffizzi cluster create
my-awesome-name.

--k8s-version=<api-version>
Specify which version of the Kubernetes API to use when creating
the cluster, formatted as [MAJOR].[MINOR]. Defaults to 1.27.
Minor versions point to the latest release of the corresponding k3s
minor version. See https://github.com/k3s-io/k3s/releases

--kubeconfig="/path/to/your/kubeconfig"
Path to kubeconfig file
Expand Down
9 changes: 5 additions & 4 deletions man/uffizzi-dev-start.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ uffizzi-dev-start - start a development environment
Currently supports skaffold.yaml files.

## FLAGS
--build="<local-or-remote>"
This option specifies whether to build images on the
local environment or on the remote Uffizzi cluster.
Possible values are "local" or "remote".
--k8s-version=<api-version>
Specify which version of the Kubernetes API to use when creating
the cluster, formatted as [MAJOR].[MINOR]. Defaults to 1.27.
Minor versions point to the latest release of the corresponding k3s
minor version. See https://github.com/k3s-io/k3s/releases

--default-repo="<container-registry-domain>"
A public or private repo used to push/pull build
Expand Down
1 change: 1 addition & 0 deletions test/uffizzi/cli/account_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class AccountTest < Minitest::Test
def setup
@account = Uffizzi::Cli::Account.new
Uffizzi::ConfigFile.write_option(:project, 'uffizzi')

sign_in
end
Expand Down
2 changes: 1 addition & 1 deletion test/uffizzi/cli/cluster_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def setup
Uffizzi.stubs(:prompt).returns(@mock_prompt)

sign_in
Uffizzi::ConfigFile.write_option(:project, 'dbp')
Uffizzi::ConfigFile.write_option(:project, 'uffizzi')
@project_slug = Uffizzi::ConfigFile.read_option(:project)
ENV['GITHUB_OUTPUT'] = '/tmp/.env'
ENV['GITHUB_ACTIONS'] = 'true'
Expand Down
2 changes: 1 addition & 1 deletion test/uffizzi/cli/dev_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def setup
Uffizzi.stubs(:prompt).returns(@mock_prompt)

sign_in
Uffizzi::ConfigFile.write_option(:project, 'dbp')
Uffizzi::ConfigFile.write_option(:project, 'uffizzi')
@project_slug = Uffizzi::ConfigFile.read_option(:project)
tmp_dir_name = (Time.now.utc.to_f * 100_000).to_i
@kubeconfig_path = "/tmp/test/#{tmp_dir_name}/test-kubeconfig.yaml"
Expand Down
2 changes: 1 addition & 1 deletion test/uffizzi/cli/preview/service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def setup
@service = Uffizzi::Cli::Preview::Service.new

sign_in
Uffizzi::ConfigFile.write_option(:project, 'dbp')
Uffizzi::ConfigFile.write_option(:project, 'uffizzi')
@project_slug = Uffizzi::ConfigFile.read_option(:project)
end

Expand Down
2 changes: 1 addition & 1 deletion test/uffizzi/cli/preview_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def setup
@preview = Uffizzi::Cli::Preview.new

sign_in
Uffizzi::ConfigFile.write_option(:project, 'dbp')
Uffizzi::ConfigFile.write_option(:project, 'uffizzi')
@project_slug = Uffizzi::ConfigFile.read_option(:project)
ENV.delete('IMAGE')
ENV.delete('CONFIG_SOURCE')
Expand Down
2 changes: 1 addition & 1 deletion test/uffizzi/cli/project/compose_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def setup
@compose = Uffizzi::Cli::Project::Compose.new

sign_in
Uffizzi::ConfigFile.write_option(:project, 'dbp')
Uffizzi::ConfigFile.write_option(:project, 'uffizzi')
@project_slug = Uffizzi::ConfigFile.read_option(:project)
ENV.delete('IMAGE')
ENV.delete('CONFIG_SOURCE')
Expand Down
Loading

0 comments on commit 7b2cadc

Please sign in to comment.