-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add new script to deploy vm with mycelium #1205
Merged
xmonader
merged 2 commits into
development
from
deployment-add-deploy-vm-with-mycelium-script
Sep 25, 2024
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
grid-client/scripts/deploy_single_vm_with_myelium/main.go
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,125 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"flag" | ||
"net" | ||
"os" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/threefoldtech/tfgrid-sdk-go/grid-client/deployer" | ||
"github.com/threefoldtech/tfgrid-sdk-go/grid-client/workloads" | ||
"github.com/threefoldtech/tfgrid-sdk-go/grid-proxy/pkg/types" | ||
"github.com/threefoldtech/zos/pkg/gridtypes" | ||
) | ||
|
||
func main() { | ||
ctx := context.Background() | ||
tf, publicKey, err := setup() | ||
if err != nil { | ||
log.Fatal().Err(err).Send() | ||
} | ||
|
||
nodeID, err := filterNode(tf) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("no available nodes found") | ||
} | ||
|
||
myceliumKey, err := workloads.RandomMyceliumKey() | ||
if err != nil { | ||
log.Debug().Err(err).Send() | ||
} | ||
|
||
network := workloads.ZNet{ | ||
Name: "test_net", | ||
Description: "network to deploy vm with mycelium", | ||
Nodes: []uint32{nodeID}, | ||
IPRange: gridtypes.NewIPNet(net.IPNet{ | ||
IP: net.IPv4(10, 1, 0, 0), | ||
Mask: net.CIDRMask(16, 32), | ||
}), | ||
AddWGAccess: false, | ||
MyceliumKeys: map[uint32][]byte{nodeID: myceliumKey}, | ||
} | ||
|
||
err = tf.NetworkDeployer.Deploy(ctx, &network) | ||
if err != nil { | ||
log.Fatal().Err(err).Send() | ||
} | ||
|
||
myceliumSeed, err := workloads.RandomMyceliumIPSeed() | ||
if err != nil { | ||
log.Debug().Err(err).Send() | ||
} | ||
|
||
vm := workloads.VM{ | ||
Name: "vm", | ||
NodeID: nodeID, | ||
NetworkName: network.Name, | ||
CPU: 2, | ||
MemoryMB: 2 * 1024, | ||
RootfsSizeMB: 10 * 1024, | ||
Planetary: true, | ||
Flist: "https://hub.grid.tf/tf-official-apps/base:latest.flist", | ||
Entrypoint: "/sbin/zinit init", | ||
MyceliumIPSeed: myceliumSeed, | ||
EnvVars: map[string]string{ | ||
"SSH_KEY": publicKey, | ||
}, | ||
} | ||
|
||
dl := workloads.NewDeployment("vm_with_mycelium", nodeID, "", nil, network.Name, nil, nil, []workloads.VM{vm}, nil, nil) | ||
err = tf.DeploymentDeployer.Deploy(context.Background(), &dl) | ||
if err != nil { | ||
log.Fatal().Err(err).Send() | ||
} | ||
|
||
dl, err = tf.State.LoadDeploymentFromGrid(ctx, nodeID, dl.Name) | ||
if err != nil { | ||
log.Fatal().Err(err).Send() | ||
} | ||
|
||
log.Info().Str("mycelium ip", dl.Vms[0].MyceliumIP).Send() | ||
} | ||
|
||
func convertGBToBytes(gb uint64) uint64 { | ||
bytes := gb * 1024 * 1024 * 1024 | ||
return bytes | ||
} | ||
|
||
func setup() (deployer.TFPluginClient, string, error) { | ||
mnemonic := os.Getenv("MNEMONICS") | ||
log.Debug().Str("MNEMONIC", mnemonic).Send() | ||
|
||
n := os.Getenv("NETWORK") | ||
log.Debug().Str("NETWORK", n).Send() | ||
|
||
var publicKeyPath string | ||
flag.StringVar(&publicKeyPath, "ssh-key", "", "path to user ssh key") | ||
flag.Parse() | ||
if publicKeyPath == "" { | ||
return deployer.TFPluginClient{}, "", errors.New("path to ssh key should not be empty") | ||
} | ||
|
||
publicKey, err := os.ReadFile(publicKeyPath) | ||
if err != nil { | ||
return deployer.TFPluginClient{}, "", err | ||
} | ||
|
||
tf, err := deployer.NewTFPluginClient(mnemonic, deployer.WithNetwork(n)) | ||
if err != nil { | ||
return deployer.TFPluginClient{}, "", err | ||
} | ||
return tf, string(publicKey), nil | ||
} | ||
|
||
func filterNode(tf deployer.TFPluginClient) (uint32, error) { | ||
f := types.NodeFilter{Status: []string{"up"}} | ||
nodes, err := deployer.FilterNodes(context.Background(), tf, f, nil, nil, []uint64{convertGBToBytes(10)}, 1) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return uint32(nodes[0].NodeID), err | ||
} |
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the directory name has
myelium
instead ofmycelium