Module: LanguageOperator::CLI::Commands::Tool::Install

Included in:
Base
Defined in:
lib/language_operator/cli/commands/tool/install.rb

Overview

Tool installation and authentication commands

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
60
61
62
63
64
65
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
180
181
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
243
244
245
246
247
248
249
250
# File 'lib/language_operator/cli/commands/tool/install.rb', line 12

def self.included(base)
  base.class_eval do
    desc 'install NAME', 'Install a tool from the registry'
    option :cluster, type: :string, desc: 'Override current cluster context'
    option :deployment_mode, type: :string, enum: %w[service sidecar], desc: 'Deployment mode (service or sidecar)'
    option :replicas, type: :numeric, desc: 'Number of replicas'
    option :dry_run, type: :boolean, default: false, desc: 'Preview without installing'
    def install(tool_name)
      handle_command_error('install tool') do
        # For dry-run mode, allow operation without a real cluster
        if options[:dry_run]
          cluster_name = options[:cluster] || 'preview'
          namespace = 'default'
        else
          cluster_name = ctx.name
          namespace = ctx.namespace
        end

        # Load tool patterns registry
        registry = Config::ToolRegistry.new
        patterns = registry.fetch

        # Resolve aliases
        tool_key = tool_name
        tool_key = patterns[tool_key]['alias'] while patterns[tool_key]&.key?('alias')

        # Look up tool in registry
        tool_config = patterns[tool_key]
        unless tool_config
          Formatters::ProgressFormatter.error("Tool '#{tool_name}' not found in registry")
          puts
          puts 'Available tools:'
          patterns.each do |key, config|
            next if config['alias']

            puts "  #{key.ljust(15)} - #{config['description']}"
          end
          exit 1
        end

        # Build template variables
        vars = {
          name: tool_name,
          namespace: namespace,
          cluster_ref: cluster_name,
          deployment_mode: options[:deployment_mode] || tool_config['deploymentMode'],
          replicas: options[:replicas] || 1,
          auth_secret: nil, # Will be set by auth command
          image: tool_config['image'],
          port: tool_config['port'],
          type: tool_config['type'],
          egress: tool_config['egress'],
          rbac: tool_config['rbac']
        }

        # Get template content - prefer registry manifest, fall back to generic template
        if tool_config['manifest']
          # Use manifest from registry (if provided in the future)
          template_content = tool_config['manifest']
        else
          # Use generic template for all tools
          template_path = File.join(__dir__, '..', '..', 'templates', 'tools', 'generic.yaml')
          template_content = File.read(template_path)
        end

        # Render template
        template = ERB.new(template_content, trim_mode: '-')
        yaml_content = template.result_with_hash(vars)

        # Dry run mode
        if options[:dry_run]
          puts "Would install tool '#{tool_name}' to cluster '#{cluster_name}':"
          puts
          puts "Display Name:    #{tool_config['displayName']}"
          puts "Description:     #{tool_config['description']}"
          puts "Deployment Mode: #{vars[:deployment_mode]}"
          puts "Replicas:        #{vars[:replicas]}"
          puts "Auth Required:   #{tool_config['authRequired'] ? 'Yes' : 'No'}"
          puts
          puts 'Generated YAML:'
          puts '---'
          puts yaml_content
          puts
          puts 'To install for real, run without --dry-run'
          return
        end

        # Check if already exists
        begin
          ctx.client.get_resource(LanguageOperator::Constants::RESOURCE_TOOL, tool_name, ctx.namespace)
          Formatters::ProgressFormatter.warn("Tool '#{tool_name}' already exists in cluster '#{ctx.name}'")
          puts
          return unless CLI::Helpers::UserPrompts.confirm('Do you want to update it?')
        rescue K8s::Error::NotFound
          # Tool doesn't exist, proceed with creation
        end

        # Install tool
        Formatters::ProgressFormatter.with_spinner("Installing tool '#{tool_name}'") do
          resource = YAML.safe_load(yaml_content, permitted_classes: [Symbol])
          ctx.client.apply_resource(resource)
        end

        puts

        # Show tool details
        format_tool_details(
          name: tool_name,
          namespace: ctx.namespace,
          cluster: ctx.name,
          status: 'Ready',
          image: tool_config['image'],
          created: Time.now.strftime('%Y-%m-%dT%H:%M:%SZ')
        )

        puts
        if tool_config['authRequired']
          puts 'This tool requires authentication. Configure it with:'
          puts pastel.dim("  langop tool auth #{tool_name}")
        else
          puts "Tool '#{tool_name}' is now available for agents to use"
        end
      end
    end

    desc 'auth NAME', 'Configure authentication for a tool'
    option :cluster, type: :string, desc: 'Override current cluster context'
    def auth(tool_name)
      handle_command_error('configure auth') do
        tool = get_resource_or_exit(LanguageOperator::Constants::RESOURCE_TOOL, tool_name,
                                    error_message: "Tool '#{tool_name}' not found. Install it first with: langop tool install #{tool_name}")

        puts "Configure authentication for tool '#{tool_name}'"
        puts

        # Determine auth type based on tool
        case tool_name
        when 'email', 'gmail'
          puts 'Email/Gmail Configuration'
          puts '-' * 40
          print 'SMTP Server: '
          smtp_server = $stdin.gets.chomp
          print 'SMTP Port (587): '
          smtp_port = $stdin.gets.chomp
          smtp_port = '587' if smtp_port.empty?
          print 'Email Address: '
          email = $stdin.gets.chomp
          print 'Password: '
          password = $stdin.noecho(&:gets).chomp
          puts

          secret_data = {
            'SMTP_SERVER' => smtp_server,
            'SMTP_PORT' => smtp_port,
            'EMAIL_ADDRESS' => email,
            'EMAIL_PASSWORD' => password
          }

        when 'github'
          puts 'GitHub Configuration'
          puts '-' * 40
          print 'GitHub Token: '
          token = $stdin.noecho(&:gets).chomp
          puts

          secret_data = {
            'GITHUB_TOKEN' => token
          }

        when 'slack'
          puts 'Slack Configuration'
          puts '-' * 40
          print 'Slack Bot Token: '
          token = $stdin.noecho(&:gets).chomp
          puts

          secret_data = {
            'SLACK_BOT_TOKEN' => token
          }

        when 'gdrive'
          puts 'Google Drive Configuration'
          puts '-' * 40
          puts 'Note: You need OAuth credentials from Google Cloud Console'
          print 'Client ID: '
          client_id = $stdin.gets.chomp
          print 'Client Secret: '
          client_secret = $stdin.noecho(&:gets).chomp
          puts

          secret_data = {
            'GDRIVE_CLIENT_ID' => client_id,
            'GDRIVE_CLIENT_SECRET' => client_secret
          }

        else
          puts 'Generic API Key Configuration'
          puts '-' * 40
          print 'API Key: '
          api_key = $stdin.noecho(&:gets).chomp
          puts

          secret_data = {
            'API_KEY' => api_key
          }
        end

        # Create secret
        secret_name = "#{tool_name}-auth"
        secret_resource = {
          'apiVersion' => 'v1',
          'kind' => 'Secret',
          'metadata' => {
            'name' => secret_name,
            'namespace' => ctx.namespace
          },
          'type' => 'Opaque',
          'stringData' => secret_data
        }

        Formatters::ProgressFormatter.with_spinner('Creating authentication secret') do
          ctx.client.apply_resource(secret_resource)
        end

        # Update tool to use secret
        tool['spec']['envFrom'] ||= []
        tool['spec']['envFrom'] << { 'secretRef' => { 'name' => secret_name } }

        Formatters::ProgressFormatter.with_spinner('Updating tool configuration') do
          ctx.client.apply_resource(tool)
        end

        Formatters::ProgressFormatter.success('Authentication configured successfully')
        puts
        puts "Tool '#{tool_name}' is now authenticated and ready to use"
      end
    end
  end
end