-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #25
- Loading branch information
Showing
10 changed files
with
281 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2024 Blink Labs Software | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
|
||
"github.com/blinklabs-io/cardano-up/pkgmgr" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func listAvailableCommand() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "list-available", | ||
Short: "List available packages", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
pm, err := pkgmgr.NewDefaultPackageManager() | ||
if err != nil { | ||
slog.Error(fmt.Sprintf("failed to create package manager: %s", err)) | ||
os.Exit(1) | ||
} | ||
packages := pm.AvailablePackages() | ||
pkgOutput := "Available packages:\n\n" | ||
for _, tmpPackage := range packages { | ||
pkgOutput += fmt.Sprintf("%s (%s) %s\n", tmpPackage.Name, tmpPackage.Version, tmpPackage.Description) | ||
} | ||
slog.Info(pkgOutput) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2024 Blink Labs Software | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package pkgmgr | ||
|
||
import "errors" | ||
|
||
// ErrOperationFailed is a placeholder error for operations that directly log errors. | ||
// It's used to signify when an operation has failed when the actual error message is | ||
// sent through the provided logger | ||
var ErrOperationFailed = errors.New("the operation has failed") | ||
|
||
// ErrMultipleInstallMethods is returned when a package's install steps specify more than one install method | ||
// on a single install step | ||
var ErrMultipleInstallMethods = errors.New("only one install method may be specified in an install step") | ||
|
||
// ErrNoInstallMethods is returned when a package's install steps include an install step which has no | ||
// recognized install method specified | ||
var ErrNoInstallMethods = errors.New("no supported install method specified on install step") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Copyright 2024 Blink Labs Software | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package pkgmgr | ||
|
||
import ( | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
type Package struct { | ||
Name string | ||
Version string | ||
Description string | ||
InstallSteps []PackageInstallStep | ||
} | ||
|
||
func (p Package) install(cfg Config) error { | ||
pkgName := fmt.Sprintf("%s-%s", p.Name, p.Version) | ||
for _, installStep := range p.InstallSteps { | ||
// Make sure only one install method is specified per install step | ||
if installStep.Docker != nil && | ||
installStep.File != nil { | ||
return ErrMultipleInstallMethods | ||
} | ||
if installStep.Docker != nil { | ||
if err := installStep.Docker.install(cfg, pkgName); err != nil { | ||
return err | ||
} | ||
} else if installStep.File != nil { | ||
if err := installStep.File.install(cfg, pkgName); err != nil { | ||
return err | ||
} | ||
} else { | ||
return ErrNoInstallMethods | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
type PackageInstallStep struct { | ||
Docker *PackageInstallStepDocker | ||
File *PackageInstallStepFile | ||
} | ||
|
||
type PackageInstallStepDocker struct { | ||
ContainerName string | ||
Image string | ||
Env map[string]string | ||
Command []string | ||
Args []string | ||
Binds []string | ||
Ports []string | ||
} | ||
|
||
func (p *PackageInstallStepDocker) install(cfg Config, pkgName string) error { | ||
containerName := fmt.Sprintf("%s-%s", pkgName, p.ContainerName) | ||
svc := DockerService{ | ||
logger: cfg.Logger, | ||
ContainerName: containerName, | ||
Image: p.Image, | ||
Env: p.Env, | ||
Command: p.Command, | ||
Args: p.Args, | ||
Binds: p.Binds, | ||
Ports: p.Ports, | ||
} | ||
if err := svc.Create(); err != nil { | ||
return err | ||
} | ||
if err := svc.Start(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
type PackageInstallStepFile struct { | ||
Filename string | ||
Content string | ||
Template bool | ||
Mode fs.FileMode | ||
} | ||
|
||
func (p *PackageInstallStepFile) install(cfg Config, pkgName string) error { | ||
// TODO: add templating support | ||
filePath := filepath.Join( | ||
cfg.ConfigDir, | ||
"data", | ||
pkgName, | ||
p.Filename, | ||
) | ||
parentDir := filepath.Dir(filePath) | ||
if err := os.MkdirAll(parentDir, fs.ModePerm); err != nil { | ||
return err | ||
} | ||
fileMode := fs.ModePerm | ||
if p.Mode > 0 { | ||
fileMode = p.Mode | ||
} | ||
if err := os.WriteFile(filePath, []byte(p.Content), fileMode); err != nil { | ||
return err | ||
} | ||
cfg.Logger.Debug(fmt.Sprintf("wrote file %s", filePath)) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2024 Blink Labs Software | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package pkgmgr | ||
|
||
var RegistryPackages = []Package{ | ||
{ | ||
Name: "cardano-node", | ||
Version: "8.7.3", | ||
Description: "Cardano node software by Input Output Global", | ||
InstallSteps: []PackageInstallStep{ | ||
{ | ||
Docker: &PackageInstallStepDocker{ | ||
ContainerName: "cardano-node", | ||
Image: "ghcr.io/blinklabs-io/cardano-node:8.7.3", | ||
Env: map[string]string{ | ||
"NETWORK": "preview", | ||
}, | ||
Ports: []string{ | ||
"3001:3001", | ||
}, | ||
}, | ||
}, | ||
{ | ||
// TODO: turn this into a template | ||
File: &PackageInstallStepFile{ | ||
Filename: "test-cardano-cli", | ||
Content: `#!/bin/bash | ||
docker exec -ti cardano-node-8.7.3-cardano-node cardano-cli $@ | ||
`, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} |