Skip to content

Commit

Permalink
remove ssh connector - only static config may be working
Browse files Browse the repository at this point in the history
  • Loading branch information
vooon committed Nov 2, 2023
1 parent 8da331c commit 8b21341
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 122 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@

# Go workspace file
go.work
fleeting-plugin-openstack
10 changes: 5 additions & 5 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ builds:
main: ./cmd/fleeting-plugin-openstack
ldflags: >-
-s -w
-X github.com/prometheus/common/version.Version={{.Version}}
-X github.com/prometheus/common/version.Revision={{.FullCommit}}
-X github.com/prometheus/common/version.Branch={{.Branch}}
-X github.com/prometheus/common/version.BuildUser=goreleaser@github-actions
-X github.com/prometheus/common/version.BuildDate={{time "20060102-15:04:05"}}
-X github.com/sardinasystems/fleeting-plugin-openstack.Version={{.Version}}
-X github.com/sardinasystems/fleeting-plugin-openstack.Revision={{.FullCommit}}
-X github.com/sardinasystems/fleeting-plugin-openstack.Branch={{.Branch}}
-X github.com/sardinasystems/fleeting-plugin-openstack.BuildUser=goreleaser@github-actions
-X github.com/sardinasystems/fleeting-plugin-openstack.BuildDate={{time "20060102-15:04:05"}}
# Set the binary output location to bin/ so archive will comply with Sensu Go Asset structure
binary: bin/{{ .ProjectName }}
Expand Down
57 changes: 0 additions & 57 deletions connector_ssh.go

This file was deleted.

1 change: 1 addition & 0 deletions etc/sample_profile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ properties:
# key_name: oskey
networks:
- network: external
security_groups: allow_ipmi_ssh
metadata:
tags: gitlab-runner
user_data: |
Expand Down
87 changes: 27 additions & 60 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ import (
var _ provider.InstanceGroup = (*InstanceGroup)(nil)

type InstanceGroup struct {
Cloud string `json:"cloud"` // cloud to use
CloudsConfig string `json:"clouds_config"` // optional: path to clouds.yaml
Name string `json:"name"` // name of the group / cluster name
ClusterID string `json:"cluster_id"` // optional: cluster id
SSHPrivateKeyFile string `json:"ssh_file"` // required: ssh key path
SSHUser string `json:"ssh_user"` // required: ssh user to login
Cloud string `json:"cloud"` // cloud to use
CloudsConfig string `json:"clouds_config"` // optional: path to clouds.yaml
Name string `json:"name"` // name of the cluster
ClusterID string `json:"cluster_id"` // optional: cluster id

size int
clusteringClient *gophercloud.ServiceClient
Expand Down Expand Up @@ -84,14 +82,12 @@ func (g *InstanceGroup) Init(ctx context.Context, log hclog.Logger, settings pro
g.ClusterID = cluster.ID
}

pemBytes, err := os.ReadFile(g.SSHPrivateKeyFile)
if err != nil {
return provider.ProviderInfo{}, fmt.Errorf("SSH Private key file required: %w", err)
if !settings.ConnectorConfig.UseStaticCredentials {
return provider.ProviderInfo{}, fmt.Errorf("Only static credentials supported")
}

g.settings = settings
g.log = log.With("name", g.Name, "cloud", g.Cloud, "cluster_name", cluster.Name, "cluster_id", cluster.ID)
g.settings.Key = pemBytes
g.size = 0

if _, err := g.getNodes(ctx, true); err != nil {
Expand All @@ -112,26 +108,24 @@ func (g *InstanceGroup) Update(ctx context.Context, update func(instance string,
return err
}

servers_, err := g.getServers(ctx, nodes_)
if err != nil {
return err
}

var reterr error
for _, node := range nodes_ {
srv, err := g.getServer(ctx, node.PhysicalID)
if err != nil {
reterr = errors.Join(reterr, err)
continue
}

state := provider.StateCreating

switch node.Status {
case "DELETING":
state = provider.StateDeleting

case "ACTIVE", "OPERATING":
state = provider.StateRunning
}

srv, ok := servers_[node.PhysicalID]
if ok {
// TODO: srv.Status?
_ = srv
if srv != nil {
state = provider.StateRunning
}
}

update(node.ID, state)
Expand Down Expand Up @@ -206,23 +200,13 @@ func (g *InstanceGroup) getNodes(ctx context.Context, initial bool) ([]nodes.Nod
return nodes, nil
}

func (g *InstanceGroup) getServers(ctx context.Context, nodelist []nodes.Node) (map[string]*servers.Server, error) {
// Ideally i'd call server list with metadata.cluster_id=id, but we can't.
// So have to query each server
var reterr error
srvs := make(map[string]*servers.Server, len(nodelist))

for _, n := range nodelist {
srv, err := servers.Get(g.computeClient, n.PhysicalID).Extract()
if err != nil {
reterr = errors.Join(reterr, err)
g.log.Error("Failed to get server", "server_id", n.PhysicalID, "error", err)
continue
}
srvs[srv.ID] = srv
func (g *InstanceGroup) getServer(ctx context.Context, id string) (*servers.Server, error) {
srv, err := servers.Get(g.computeClient, id).Extract()
if errors.Is(err, &gophercloud.ErrResourceNotFound{}) {
return nil, nil
}

return srvs, reterr
return srv, err
}

func (g *InstanceGroup) ConnectInfo(ctx context.Context, instanceID string) (provider.ConnectInfo, error) {
Expand All @@ -231,17 +215,22 @@ func (g *InstanceGroup) ConnectInfo(ctx context.Context, instanceID string) (pro
return provider.ConnectInfo{}, fmt.Errorf("Failed to get node %s: %w", instanceID, err)
}

srv, err := servers.Get(g.computeClient, node.PhysicalID).Extract()
srv, err := g.getServer(ctx, node.PhysicalID)
if err != nil {
return provider.ConnectInfo{}, fmt.Errorf("Failed to get server %s: %w", node.PhysicalID, err)
}
if srv == nil {
return provider.ConnectInfo{}, fmt.Errorf("Server not found %s: %w", node.PhysicalID, os.ErrNotExist)
}

if srv.Status != "ACTIVE" {
return provider.ConnectInfo{}, fmt.Errorf("instance status is not active: %s", srv.Status)
}

// TODO: get image metadata and get os_admin_user

g.log.Info("srv", "srv", srv)

info := provider.ConnectInfo{
ConnectorConfig: g.settings.ConnectorConfig,
InternalAddr: srv.AccessIPv4,
Expand All @@ -251,28 +240,6 @@ func (g *InstanceGroup) ConnectInfo(ctx context.Context, instanceID string) (pro
// TODO: get from image meta
info.OS = "linux"
info.Arch = "amd64"
if info.Username == "" {
info.Username = g.SSHUser
}

if info.UseStaticCredentials {
return info, nil
}

if info.Protocol == "" {
info.Protocol = provider.ProtocolSSH
}

switch info.Protocol {
case provider.ProtocolSSH:
err = g.ssh(ctx, &info)

case provider.ProtocolWinRM:
err = fmt.Errorf("winrm not supported")
}
if err != nil {
return provider.ConnectInfo{}, err
}

return info, nil
}
Expand Down

0 comments on commit 8b21341

Please sign in to comment.