Skip to content

Commit

Permalink
Merge branch 'manifest-remove'
Browse files Browse the repository at this point in the history
  • Loading branch information
jlmorris3827 committed Feb 11, 2019
2 parents f2c96d2 + a1b5c32 commit c92eadb
Show file tree
Hide file tree
Showing 44 changed files with 1,241 additions and 2,543 deletions.
20 changes: 13 additions & 7 deletions k8s-prov/dind/dind.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func GetLocalAddr() (string, error) {
defer conn.Close()

localAddr := conn.LocalAddr().(*net.UDPAddr)

return localAddr.IP.String(), nil
}

Expand All @@ -36,23 +35,30 @@ func GetDockerNetworkName(clusterName string) string {
}

//CreateDINDCluster creates kubernetes cluster on local mac
func CreateDINDCluster(group, name string) error {
os.Setenv("DIND_LABEL", name)
func CreateDINDCluster(clusterName, kconfName string) error {

os.Setenv("DIND_LABEL", clusterName)
os.Setenv("CLUSTER_ID", getClusterID())
log.DebugLog(log.DebugLevelMexos, "CreateDINDCluster via dind-cluster-v1.13.sh", "name", name, "clusterid", getClusterID())
log.DebugLog(log.DebugLevelMexos, "CreateDINDCluster via dind-cluster-v1.13.sh", "name", clusterName, "clusterid", getClusterID())

out, err := sh.Command("dind-cluster-v1.13.sh", "up").CombinedOutput()
if err != nil {
return fmt.Errorf("ERROR creating Dind Cluster: [%s] %v", out, err)
}
log.DebugLog(log.DebugLevelMexos, "Finished CreateDINDCluster", "name", name)
log.DebugLog(log.DebugLevelMexos, "Finished CreateDINDCluster", "name", clusterName)

//now set the k8s config
out, err = sh.Command("kubectl", "config", "use-context", "dind-"+name+"-"+getClusterID()).CombinedOutput()
out, err = sh.Command("kubectl", "config", "use-context", "dind-"+clusterName+"-"+getClusterID()).CombinedOutput()
if err != nil {
return fmt.Errorf("ERROR setting kube config context: [%s] %v", out, err)
}

//copy kubeconfig locally
log.DebugLog(log.DebugLevelMexos, "locally copying kubeconfig", "kconfName", kconfName)
home := os.Getenv("HOME")
out, err = sh.Command("cp", home+"/.kube/config", home+"/.kube/"+kconfName).CombinedOutput()
if err != nil {
return fmt.Errorf("%s %v", out, err)
}
return nil
}

Expand Down
131 changes: 42 additions & 89 deletions mexos/agent.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package mexos

import (
"encoding/json"
"fmt"
"strings"

valid "github.com/asaskevich/govalidator"
"github.com/mobiledgex/edge-cloud-infra/openstack-tenant/agent/cloudflare"
"github.com/mobiledgex/edge-cloud/edgeproto"
"github.com/mobiledgex/edge-cloud/integration/process"
"github.com/mobiledgex/edge-cloud/log"
)
Expand All @@ -16,34 +17,35 @@ func runLocalMexAgent() error {
return localMexos.Start("/tmp/mexosagent.log")
}

//RunMEXAgentManifest runs the MEX agent on the RootLB. It first registers FQDN to cloudflare domain registry if not already registered.
//RunMEXAgent runs the MEX agent on the RootLB. It first registers FQDN to cloudflare domain registry if not already registered.
// It then obtains certficiates from Letsencrypt, if not done yet. Then it runs the docker instance of MEX agent
// on the RootLB. It can be told to manually pull image from docker repository. This allows upgrading with new image.
// It uses MEX private docker repository. If an instance is running already, we don't start another one.
func RunMEXAgentManifest(mf *Manifest) error {
func RunMEXAgent(rootLBName string, cloudletKey *edgeproto.CloudletKey) error {
log.DebugLog(log.DebugLevelMexos, "run mex agent")

if IsLocalDIND(mf) {
if CloudletIsLocalDIND() {
return runLocalMexAgent()
}
fqdn := mf.Spec.RootLB
if CloudletIsPublicCloud() {
log.DebugLog(log.DebugLevelMexos, "skip mex agent for public cloud") //TODO: maybe later we will actually have agent on public cloud
return nil
}
fqdn := rootLBName
//fqdn is that of the machine/kvm-instance running the agent
if !valid.IsDNSName(fqdn) {
return fmt.Errorf("fqdn %s is not valid", fqdn)
}
if err := setPlatConfManifest(mf); err != nil {
return fmt.Errorf("can't set plat conf, %v", err)
}
sd, err := GetServerDetails(mf, fqdn)
sd, err := GetServerDetails(fqdn)
if err == nil {
if sd.Name == fqdn {
log.DebugLog(log.DebugLevelMexos, "server with same name as rootLB exists", "fqdn", fqdn)
rootLB, err := getRootLB(fqdn)
if err != nil {
return fmt.Errorf("cannot find rootlb %s", fqdn)
}
//return RunMEXOSAgentContainer(mf, rootLB)
return RunMEXOSAgentService(mf, rootLB)
//return RunMEXOSAgentContainer(rootLB)
return RunMEXOSAgentService(rootLB)
}
}
log.DebugLog(log.DebugLevelMexos, "about to create mex agent", "fqdn", fqdn)
Expand All @@ -54,50 +56,44 @@ func RunMEXAgentManifest(mf *Manifest) error {
if rootLB == nil {
return fmt.Errorf("cannot run mex agent manifest, rootLB is null")
}
if mf.Spec.ExternalNetwork == "" {
return fmt.Errorf("missing external network")
}
if mf.Spec.Agent.Image == "" {
if GetCloudletOSImage() == "" {
return fmt.Errorf("missing agent image")
}
if mf.Metadata.Name == "" {
return fmt.Errorf("missing name")
}
log.DebugLog(log.DebugLevelMexos, "record platform config")
err = EnableRootLB(mf, rootLB)
err = EnableRootLB(rootLB, cloudletKey)
if err != nil {
log.DebugLog(log.DebugLevelMexos, "can't enable agent", "name", rootLB.Name)
return fmt.Errorf("Failed to enable root LB %v", err)
}
err = WaitForRootLB(mf, rootLB)
err = WaitForRootLB(rootLB)
if err != nil {
log.DebugLog(log.DebugLevelMexos, "timeout waiting for agent to run", "name", rootLB.Name)
return fmt.Errorf("Error waiting for rootLB %v", err)
}
if err := SetupSSHUser(mf, rootLB, sshUser); err != nil {
if err := SetupSSHUser(rootLB, sshUser); err != nil {
return err
}
if err = ActivateFQDNA(mf, rootLB, rootLB.Name); err != nil {
if err = ActivateFQDNA(rootLB, rootLB.Name); err != nil {
return err
}
log.DebugLog(log.DebugLevelMexos, "FQDN A record activated", "name", rootLB.Name)
err = AcquireCertificates(mf, rootLB, rootLB.Name) //fqdn name may be different than rootLB.Name
err = AcquireCertificates(rootLB, rootLB.Name) //fqdn name may be different than rootLB.Name
if err != nil {
return fmt.Errorf("can't acquire certificate for %s, %v", rootLB.Name, err)
}
log.DebugLog(log.DebugLevelMexos, "acquired certificates from letsencrypt", "name", rootLB.Name)
err = GetHTPassword(mf, rootLB)
err = GetHTPassword(rootLB.Name)
if err != nil {
return fmt.Errorf("can't download htpassword %v", err)
}
//return RunMEXOSAgentContainer(mf, rootLB)
return RunMEXOSAgentService(mf, rootLB)
return RunMEXOSAgentService(rootLB)
}

func RunMEXOSAgentService(mf *Manifest, rootLB *MEXRootLB) error {
func RunMEXOSAgentService(rootLB *MEXRootLB) error {
//TODO check if agent is running before restarting again.
log.DebugLog(log.DebugLevelMexos, "run mexosagent service")
client, err := GetSSHClient(mf, rootLB.Name, mf.Values.Network.External, sshUser)
client, err := GetSSHClient(rootLB.Name, GetCloudletExternalNetwork(), sshUser)
if err != nil {
return err
}
Expand All @@ -113,7 +109,7 @@ func RunMEXOSAgentService(mf *Manifest, rootLB *MEXRootLB) error {
{"/usr/local/bin", "mexosagent"},
{"/lib/systemd/system", "mexosagent.service"},
} {
cmd := fmt.Sprintf("sudo scp -o %s -o %s -i id_rsa_mex mobiledgex@%s:files-repo/mobiledgex/%s %s", sshOpts[0], sshOpts[1], mf.Values.Registry.Name, dest.name, dest.path)
cmd := fmt.Sprintf("sudo scp -o %s -o %s -i id_rsa_mex mobiledgex@%s:files-repo/mobiledgex/%s %s", sshOpts[0], sshOpts[1], GetCloudletRegistryFileServer(), dest.name, dest.path)
out, err := client.Output(cmd)
if err != nil {
log.InfoLog("error: cannot download from registry", "fn", dest.name, "path", dest.path, "error", err, "out", out)
Expand All @@ -136,47 +132,41 @@ func RunMEXOSAgentService(mf *Manifest, rootLB *MEXRootLB) error {
return nil
}

func RunMEXOSAgentContainer(mf *Manifest, rootLB *MEXRootLB) error {
if mexEnv(mf, "MEX_DOCKER_REG_PASS") == "" {
return fmt.Errorf("empty docker registry pass env var")
}
client, err := GetSSHClient(mf, rootLB.Name, mf.Values.Network.External, sshUser)
func RunMEXOSAgentContainer(rootLB *MEXRootLB) error {
client, err := GetSSHClient(rootLB.Name, GetCloudletExternalNetwork(), sshUser)
if err != nil {
return err
}
//XXX rewrite this with --format {{.Names}}
cmd := fmt.Sprintf("docker ps --filter ancestor=%s --format {{.Names}}", mf.Spec.Agent.Image)
cmd := fmt.Sprintf("docker ps --filter ancestor=%s --format {{.Names}}", GetCloudletAgentContainerImage())
out, err := client.Output(cmd)
if err == nil && strings.Contains(out, rootLB.Name) {
//agent docker instance exists
//XXX check better
log.DebugLog(log.DebugLevelMexos, "agent docker instance already running")
return nil
}
cmd = fmt.Sprintf("echo %s > .docker-pass", mexEnv(mf, "MEX_DOCKER_REG_PASS"))
cmd = fmt.Sprintf("echo %s > .docker-pass", GetCloudletDockerPass())
out, err = client.Output(cmd)
if err != nil {
return fmt.Errorf("can't store docker pass, %s, %v", out, err)
}
log.DebugLog(log.DebugLevelMexos, "seeded docker registry password")
dockerinstanceName := fmt.Sprintf("%s-%s", mf.Metadata.Name, rootLB.Name)
if mf.Spec.DockerRegistry == "" {
log.DebugLog(log.DebugLevelMexos, "warning, empty docker registry spec, using default.")
mf.Spec.DockerRegistry = mf.Values.Registry.Docker
}
cmd = fmt.Sprintf("cat .docker-pass| docker login -u mobiledgex --password-stdin %s", mf.Spec.DockerRegistry)
dockerinstanceName := fmt.Sprintf("%s-%s", "mexos", rootLB.Name)

cmd = fmt.Sprintf("cat .docker-pass| docker login -u mobiledgex --password-stdin %s", GetCloudletDockerRegistry())
out, err = client.Output(cmd)
if err != nil {
return fmt.Errorf("error docker login at %s, %s, %s, %v", rootLB.Name, cmd, out, err)
}
log.DebugLog(log.DebugLevelMexos, "docker login ok")
cmd = fmt.Sprintf("docker pull %s", mf.Spec.Agent.Image) //probably redundant
cmd = fmt.Sprintf("docker pull %s", GetCloudletAgentContainerImage()) //probably redundant
out, err = client.Output(cmd)
if err != nil {
return fmt.Errorf("error pulling docker image at %s, %s, %s, %v", rootLB.Name, cmd, out, err)
}
log.DebugLog(log.DebugLevelMexos, "pulled agent image ok")
cmd = fmt.Sprintf("docker run -d --rm --name %s --net=host -v `pwd`:/var/www/.cache -v /etc/ssl/certs:/etc/ssl/certs %s -debug", dockerinstanceName, mf.Spec.Agent.Image)
cmd = fmt.Sprintf("docker run -d --rm --name %s --net=host -v `pwd`:/var/www/.cache -v /etc/ssl/certs:/etc/ssl/certs %s -debug", dockerinstanceName, GetCloudletAgentContainerImage())
out, err = client.Output(cmd)
if err != nil {
return fmt.Errorf("error running dockerized agent on RootLB %s, %s, %s, %v", rootLB.Name, cmd, out, err)
Expand All @@ -185,54 +175,17 @@ func RunMEXOSAgentContainer(mf *Manifest, rootLB *MEXRootLB) error {
return nil
}

//UpdateMEXAgentManifest upgrades the mex agent
func UpdateMEXAgentManifest(mf *Manifest) error {
log.DebugLog(log.DebugLevelMexos, "update mex agent")
err := RemoveMEXAgentManifest(mf)
if err != nil {
return err
}
// Force pulling a potentially newer docker image
return RunMEXAgentManifest(mf)
}
//RunMEXAgentCloudletKey calls MEXPlatformInit with templated manifest
func RunMEXAgentCloudletKey(rootLBName string, cloudletKeyStr string) error {

//RemoveMEXAgentManifest deletes mex agent docker instance
func RemoveMEXAgentManifest(mf *Manifest) error {
log.DebugLog(log.DebugLevelMexos, "deleting mex agent")
//XXX we are deleting server kvm!!!
err := DeleteServer(mf, mf.Spec.RootLB)
force := strings.Contains(mf.Spec.Flags, "force")
clk := edgeproto.CloudletKey{}
err := json.Unmarshal([]byte(cloudletKeyStr), &clk)
if err != nil {
if !force {
return err
}
log.DebugLog(log.DebugLevelMexos, "forced to continue, deleting mex agent error", "error", err, "rootLB", mf.Spec.RootLB)
}
log.DebugLog(log.DebugLevelMexos, "removed rootlb", "name", mf.Spec.RootLB)
sip, err := GetServerIPAddr(mf, mf.Values.Network.External, mf.Spec.RootLB)
if err := DeleteSecurityRule(mf, sip); err != nil {
log.DebugLog(log.DebugLevelMexos, "warning, cannot delete security rule", "error", err, "server ip", sip)
}
if mf.Metadata.DNSZone == "" {
return fmt.Errorf("missing dns zone in manifest, metadata %v", mf.Metadata)
}
if cerr := cloudflare.InitAPI(mexEnv(mf, "MEX_CF_USER"), mexEnv(mf, "MEX_CF_KEY")); cerr != nil {
return fmt.Errorf("cannot init cloudflare api, %v", cerr)
return fmt.Errorf("can't unmarshal json cloudletkey %s, %v", cloudletKeyStr, err)
}
recs, derr := cloudflare.GetDNSRecords(mf.Metadata.DNSZone)
fqdn := mf.Spec.RootLB
if derr != nil {
return fmt.Errorf("can not get dns records for %s, %v", fqdn, derr)
log.DebugLog(log.DebugLevelMexos, "unmarshalled cloudletkeystr", "cloudletkey", clk)
if clk.Name == "" || clk.OperatorKey.Name == "" {
return fmt.Errorf("invalid cloudletkeystr %s", cloudletKeyStr)
}
for _, rec := range recs {
if rec.Type == "A" && rec.Name == fqdn {
err = cloudflare.DeleteDNSRecord(mf.Metadata.DNSZone, rec.ID)
if err != nil {
return fmt.Errorf("cannot delete dns record id %s Zone %s, %v", rec.ID, mf.Metadata.DNSZone, err)
}
}
}
log.DebugLog(log.DebugLevelMexos, "removed DNS A record", "FQDN", fqdn)
//TODO remove mex-k8s internal nets and router
return nil
return RunMEXAgent(rootLBName, &clk)
}
Loading

0 comments on commit c92eadb

Please sign in to comment.