Class: Kamal::Cli::Dev
- Inherits:
-
Thor
- Object
- Thor
- Kamal::Cli::Dev
- Defined in:
- lib/kamal/cli/dev.rb
Instance Method Summary collapse
- #build ⇒ Object
- #deploy(name = nil) ⇒ Object
- #init ⇒ Object
- #list ⇒ Object
- #push(image_ref = nil) ⇒ Object
- #remove(name = nil) ⇒ Object
- #status(name = nil) ⇒ Object
- #stop(name = nil) ⇒ Object
Instance Method Details
#build ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/kamal/cli/dev.rb', line 66 def build config = load_config registry = Kamal::Dev::Registry.new(config) builder = Kamal::Dev::Builder.new(config, registry) # Check Docker is available unless builder.docker_available? puts "❌ Error: Docker is required to build images" puts " Please install Docker Desktop or Docker Engine" exit 1 end # Check registry credentials unless registry.credentials_present? username_var = config.registry["username"] password_var = config.registry["password"] puts "❌ Error: Registry credentials not found" puts " Please set #{username_var} and #{password_var} in .kamal/secrets" exit 1 end puts "🔨 Building image for '#{config.service}'" puts # Authenticate with registry puts "Authenticating with registry..." begin builder.login puts "✓ Logged in to #{registry.server}" rescue Kamal::Dev::RegistryError => e puts "❌ Registry login failed: #{e.message}" exit 1 end puts # Determine build source from config or options # Priority: CLI options > config.build > defaults dockerfile = [:dockerfile] context = [:context] # If not provided via CLI, check config unless dockerfile && context if config.build_source_type == :devcontainer # Parse devcontainer.json to get Dockerfile and context devcontainer_path = config.build_source_path parser = Kamal::Dev::DevcontainerParser.new(devcontainer_path) if parser.uses_compose? # Extract from compose file compose_file = parser.compose_file_path compose_parser = Kamal::Dev::ComposeParser.new(compose_file) main_service = compose_parser.main_service dockerfile ||= compose_parser.service_dockerfile(main_service) context ||= compose_parser.service_build_context(main_service) else # For non-compose devcontainers, this will be implemented later raise Kamal::Dev::ConfigurationError, "Non-compose devcontainer builds not yet supported. Use build.dockerfile instead." end elsif config.build_source_type == :dockerfile dockerfile ||= config.build["dockerfile"] context ||= config.build_context else dockerfile ||= "Dockerfile" context ||= "." end end tag = [:tag] puts "Building image..." puts " Dockerfile: #{dockerfile}" puts " Context: #{context}" puts " Destination: #{config.image}" puts " Tag: #{tag || "(auto-generated timestamp)"}" puts begin # Use config.image as base name, registry will handle full path image_ref = builder.build( dockerfile: dockerfile, context: context, tag: tag, image_base: config.image ) puts puts "✓ Built image: #{image_ref}" rescue Kamal::Dev::BuildError => e puts "❌ Build failed: #{e.message}" exit 1 end puts # Push image (unless --skip-push) unless [:skip_push] puts "Pushing image to registry..." begin builder.push(image_ref) puts puts "✓ Pushed image: #{image_ref}" rescue Kamal::Dev::BuildError => e puts "❌ Push failed: #{e.message}" exit 1 end puts end puts "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" puts "✅ Build complete!" puts "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" puts puts "Image: #{image_ref}" puts end |
#deploy(name = nil) ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/kamal/cli/dev.rb', line 250 def deploy(name = nil) config = load_config count = [:count] || 1 # Validate git configuration if git clone is enabled validate_git_config!(config) puts "🚀 Deploying #{count} devcontainer workspace(s) for '#{config.service}'" puts # Step 1: Check if using Docker Compose # For new format: use build.devcontainer path # For old format: use image path (backward compatibility) devcontainer_path = config.build_source_path || config.image devcontainer_path = [:from] if [:from] != ".devcontainer/devcontainer.json" # CLI override parser = Kamal::Dev::DevcontainerParser.new(devcontainer_path) uses_compose = parser.uses_compose? if uses_compose deploy_compose_stack(config, count, parser) else deploy_single_container(config, count) end end |
#init ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/kamal/cli/dev.rb', line 29 def init config_path = "config/dev.yml" if File.exist?(config_path) print "⚠️ #{config_path} already exists. Overwrite? (y/n): " response = $stdin.gets.chomp.downcase return unless response == "y" || response == "yes" end # Create config directory if it doesn't exist FileUtils.mkdir_p("config") unless Dir.exist?("config") # Copy template to config/dev.yml template_path = File.("../../dev/templates/dev.yml", __FILE__) FileUtils.cp(template_path, config_path) puts "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" puts "✅ Created #{config_path}" puts "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" puts puts "Next steps:" puts puts "1. Edit #{config_path} with your cloud provider credentials" puts "2. Create .kamal/secrets file with your secrets:" puts " export UPCLOUD_USERNAME=\"your-username\"" puts " export UPCLOUD_PASSWORD=\"your-password\"" puts puts "3. Deploy your first workspace:" puts " kamal dev deploy --count 3" puts end |
#list ⇒ Object
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 |
# File 'lib/kamal/cli/dev.rb', line 600 def list state_manager = get_state_manager deployments = state_manager.list_deployments if deployments.empty? puts "No deployments found" return end case [:format] when "json" puts JSON.pretty_generate(deployments) when "yaml" puts YAML.dump(deployments) else # Table format (default) print_table(deployments) end end |
#push(image_ref = nil) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/kamal/cli/dev.rb', line 182 def push(image_ref = nil) config = load_config registry = Kamal::Dev::Registry.new(config) builder = Kamal::Dev::Builder.new(config, registry) # Use provided image or generate from config image_ref ||= begin puts "No image specified. Using image from config..." tag = registry. registry.image_tag(config.image, tag) end # Check Docker is available unless builder.docker_available? puts "❌ Error: Docker is required to push images" puts " Please install Docker Desktop or Docker Engine" exit 1 end # Check registry credentials unless registry.credentials_present? username_var = config.registry["username"] password_var = config.registry["password"] puts "❌ Error: Registry credentials not found" puts " Please set #{username_var} and #{password_var} in .kamal/secrets" exit 1 end puts "📤 Pushing image '#{image_ref}'" puts # Authenticate with registry puts "Authenticating with registry..." begin builder.login puts "✓ Logged in to #{registry.server}" rescue Kamal::Dev::RegistryError => e puts "❌ Registry login failed: #{e.message}" exit 1 end puts # Push image puts "Pushing image to registry..." begin builder.push(image_ref) puts puts "✓ Pushed image: #{image_ref}" rescue Kamal::Dev::BuildError => e puts "❌ Push failed: #{e.message}" exit 1 end puts puts "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" puts "✅ Push complete!" puts "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" puts puts "Image: #{image_ref}" puts end |
#remove(name = nil) ⇒ Object
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 |
# File 'lib/kamal/cli/dev.rb', line 623 def remove(name = nil) state_manager = get_state_manager deployments = state_manager.list_deployments if deployments.empty? puts "No deployments found" return end # Load config and provider if available provider = nil begin config = load_config provider = get_provider(config) rescue => e puts "⚠️ Warning: Could not load config (#{e.message}). VMs will not be destroyed." end if [:all] # Confirmation prompt unless [:force] print "⚠️ This will destroy #{deployments.size} VM(s) and remove all containers. Continue? (y/n): " response = $stdin.gets.chomp.downcase return unless response == "y" || response == "yes" end # Remove all containers count = 0 deployments.each do |container_name, deployment| if provider puts "Destroying VM #{deployment["vm_id"]} (#{deployment["vm_ip"]})..." begin stop_container(deployment["vm_ip"], container_name) rescue nil end provider.destroy_vm(deployment["vm_id"]) end state_manager.remove_deployment(container_name) count += 1 end puts "Removed #{count} deployment(s)" elsif name # Remove specific container unless deployments.key?(name) puts "Container '#{name}' not found" return end deployment = deployments[name] # Confirmation prompt unless [:force] print "⚠️ This will destroy VM #{deployment["vm_id"]} and remove container '#{name}'. Continue? (y/n): " response = $stdin.gets.chomp.downcase return unless response == "y" || response == "yes" end if provider puts "Destroying VM #{deployment["vm_id"]} (#{deployment["vm_ip"]})..." begin stop_container(deployment["vm_ip"], name) rescue nil end provider.destroy_vm(deployment["vm_id"]) end state_manager.remove_deployment(name) puts "Container '#{name}' removed" else puts "Error: Please specify a container name or use --all flag" end end |
#status(name = nil) ⇒ Object
700 701 702 703 |
# File 'lib/kamal/cli/dev.rb', line 700 def status(name = nil) puts "Status command called" # Implementation will be added in later tasks end |
#stop(name = nil) ⇒ Object
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 |
# File 'lib/kamal/cli/dev.rb', line 562 def stop(name = nil) state_manager = get_state_manager deployments = state_manager.list_deployments if deployments.empty? puts "No deployments found" return end if [:all] # Stop all containers count = 0 deployments.each do |container_name, deployment| puts "Stopping #{container_name} on #{deployment["vm_ip"]}..." stop_container(deployment["vm_ip"], container_name) state_manager.update_deployment_status(container_name, "stopped") count += 1 end puts "Stopped #{count} container(s)" elsif name # Stop specific container unless deployments.key?(name) puts "Container '#{name}' not found" return end deployment = deployments[name] puts "Stopping #{name} on #{deployment["vm_ip"]}..." stop_container(deployment["vm_ip"], name) state_manager.update_deployment_status(name, "stopped") puts "Container '#{name}' stopped" else puts "Error: Please specify a container name or use --all flag" end end |