Skip to content

Commit

Permalink
Merge pull request #2998 from Polber/jkinard/2217
Browse files Browse the repository at this point in the history
Issue 2217 - AgentAutoUpgrade: Improve support for running the anax container on linux edge node
  • Loading branch information
linggao committed Feb 3, 2022
2 parents 77d1de5 + 685e24c commit 2472d40
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 80 deletions.
210 changes: 151 additions & 59 deletions agent-install/agent-install.sh

Large diffs are not rendered by default.

74 changes: 62 additions & 12 deletions anax-in-container/horizon-container
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,9 @@ canonicalPath() {
}

# Bash cant do AND of cmd and checking a string (afaik), so bury it in this function
isMacAndDefaultIndex() {
if isMacos; then
if [[ "$INDEX_NUM" == "1" ]]; then
return 0
else
return 1
fi
isDefaultIndex() {
if [[ "$INDEX_NUM" == "1" ]]; then
return 0
else
return 1
fi
Expand Down Expand Up @@ -124,6 +120,56 @@ checkRequirements() {
fi
}

# Creates and copies a file similar to os-release on Linux systems to the docker container
# for anax-in-container installs. This allows the container to know which horizon-cli package
# to download during automatic upgrades.
copyHostOSInfo() {
# determine host os, distribution and version
local os='linux'
local distro=''
local distroVersionNum=''
if isMacos; then
os='macos'
distro=$(sw_vers | grep ProductName | awk '{ print$2 }')
distroVersionNum=$(sw_vers | grep ProductVersion | awk '{ print$2 }')
elif [[ -f /etc/os-release ]]; then
. /etc/os-release
distro=$ID
distroVersionNum=$VERSION_ID
elif [[ -f /etc/lsb-release ]]; then
. /etc/lsb-release
distro=$DISTRIB_ID
distroVersionNum=$DISTRIB_RELEASE
else
echo "Cannot detect OS version"
exit 2
fi

# determine host architecture and convert to hzn-friendly form
local image_arch=$(uname -m)
if [[ $image_arch == 'x86_64' ]]; then
image_arch='amd64'
elif [[ $image_arch == 'aarch64' ]]; then
image_arch='arm64'
fi

# write os, distro, distro version and architecture to file
echo "OS=$os" > host-os-release
echo "DISTRO=$distro" >> host-os-release
echo "DISTRO_VERSION_NUM=$distroVersionNum" >> host-os-release
echo "ARCH=$image_arch" >> host-os-release

# copy file to docker container
docker cp host-os-release $DOCKER_NAME:/etc/host-os-release
if [[ $? -ne 0 ]]; then
echo "Could not copy host os info into agent docker container"
exit 2
fi

# cleanup
rm host-os-release
}

# Pull the latest horizon docker image, get some prereqs established, and start the image
# First arg is optional host default file, second arg has value 'updating' if we are called by restart
start() {
Expand Down Expand Up @@ -217,10 +263,10 @@ start() {
# $HC_DOCKER_IMAGE and $HC_DOCKER_TAG are intentionally undocumented env vars that allow us to test the staging version of the docker image

# get the architecture from hzn
arch=$(LANG=en_US hzn architecture)
if [ $? -ne 0 ]; then
arch="amd64"
fi
arch=$(LANG=en_US hzn architecture)
if [ $? -ne 0 ]; then
arch="amd64"
fi

dockerImage='openhorizon/'$arch'_anax'
dockerTag='latest'
Expand Down Expand Up @@ -291,13 +337,17 @@ start() {
if [[ "$2" == "updating" ]]; then
echo "Horizon agent updated/restarted successfully."
else
if isMacAndDefaultIndex; then
if isDefaultIndex; then
# hzn on mac sets HORIZON_URL correctly by default for index 1, so the user does not need to do it
echo "Horizon agent started successfully. Now use 'hzn node list', 'hzn register ...', and 'hzn agreement list'"
else
echo "Horizon agent started successfully. Now export HORIZON_URL=http://localhost:$HORIZON_AGENT_PORT, then use 'hzn node list', 'hzn register ...', and 'hzn agreement list'"
fi
fi

# now copy over the host OS info to the container so that automatic upgrades can be enabled
copyHostOSInfo

} # end start()


Expand Down
3 changes: 2 additions & 1 deletion cli/hzn.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ Environment Variables:
removeNodeUnregister := unregisterCmd.Flag("remove", msgPrinter.Sprintf("Also remove this node resource from the Horizon exchange (because you no longer want to use this node with Horizon).")).Short('r').Bool()
deepCleanUnregister := unregisterCmd.Flag("deep-clean", msgPrinter.Sprintf("Also remove all the previous registration information. Use it only after the 'hzn unregister' command failed. Please capture the logs by running 'hzn eventlog list -a -l' command before using this flag.")).Short('D').Bool()
timeoutUnregister := unregisterCmd.Flag("timeout", msgPrinter.Sprintf("The number of minutes to wait for unregistration to complete. The default is zero which will wait forever.")).Short('t').Default("0").Int()
containerUnregister := unregisterCmd.Flag("container", msgPrinter.Sprintf("Perform a deep clean on a node running in a container. This flag must be used with -D and only if the agent was installed as anax-in-container.")).Short('C').Bool()

userinputCmd := app.Command("userinput | u", msgPrinter.Sprintf("List or manage the service user inputs that are currently registered on this Horizon edge node.")).Alias("u").Alias("userinput")
userinputAddCmd := userinputCmd.Command("add", msgPrinter.Sprintf("Add a new user input object or overwrite the current user input object for this Horizon edge node."))
Expand Down Expand Up @@ -1213,7 +1214,7 @@ Environment Variables:
case serviceConfigStateActiveCmd.FullCommand():
service.Resume(*resumeAllServices, *resumeServiceOrg, *resumeServiceName, *resumeServiceVersion)
case unregisterCmd.FullCommand():
unregister.DoIt(*forceUnregister, *removeNodeUnregister, *deepCleanUnregister, *timeoutUnregister)
unregister.DoIt(*forceUnregister, *removeNodeUnregister, *deepCleanUnregister, *timeoutUnregister, *containerUnregister)
case statusCmd.FullCommand():
status.DisplayStatus(*statusLong, false)
case eventlogListCmd.FullCommand():
Expand Down
21 changes: 15 additions & 6 deletions cli/unregister/unregister.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ type ApiAttributes struct {
}

// DoIt unregisters this Horizon edge node and resets it so it can be registered again
func DoIt(forceUnregister, removeNodeUnregister bool, deepClean bool, timeout int) {
func DoIt(forceUnregister, removeNodeUnregister bool, deepClean bool, timeout int, container bool) {
// get message printer
msgPrinter := i18n.GetMessagePrinter()

if !deepClean && container {
cliutils.Fatal(cliutils.CLI_INPUT_ERROR, msgPrinter.Sprintf("Cannot use -C flag if not performing a deep clean. Must specify -D flag to use -C flag."))
}

if !forceUnregister {
cliutils.ConfirmRemove(msgPrinter.Sprintf("Are you sure you want to unregister this Horizon node?"))
}
Expand All @@ -54,7 +58,7 @@ func DoIt(forceUnregister, removeNodeUnregister bool, deepClean bool, timeout in

// still allow deep clean, just in case the node is in a strange state and the user really want to clean it up.
if deepClean {
if err := DeepClean(); err != nil {
if err := DeepClean(container); err != nil {
fmt.Println(err.Error())
}
}
Expand All @@ -75,7 +79,7 @@ func DoIt(forceUnregister, removeNodeUnregister bool, deepClean bool, timeout in
msgPrinter.Println()
}

if err := DeepClean(); err != nil {
if err := DeepClean(container); err != nil {
fmt.Println(err.Error())
} else {
unregErr = nil
Expand Down Expand Up @@ -180,7 +184,7 @@ func DeleteHorizonNode(removeNodeUnregister bool, deepClean bool, timeout int) e
}

// remove local db, policy files and all the service containers
func DeepClean() error {
func DeepClean(agentInContainer bool) error {

// detect the node type
nodeType := persistence.DEVICE_TYPE_DEVICE
Expand All @@ -206,7 +210,7 @@ func DeepClean() error {
cliutils.RunCmd(nil, "pkill", "-f", "/usr/horizon/bin/anax")

} else {
if runtime.GOOS == "darwin" {
if runtime.GOOS == "darwin" || agentInContainer {
containerIdx, err := cliutils.GetHorizonContainerIndex()
containerName := fmt.Sprintf("horizon%d", containerIdx)
if err != nil {
Expand All @@ -231,8 +235,13 @@ func DeepClean() error {
if errBytes != nil && len(errBytes) > 0 {
return fmt.Errorf(msgPrinter.Sprintf("Error during removing policy files from horizon container: %s", errBytes))
}
outBytes, errBytes = cliutils.RunCmd(nil, "/bin/sh", "-c",
if runtime.GOOS == "darwin" {
outBytes, errBytes = cliutils.RunCmd(nil, "/bin/sh", "-c",
fmt.Sprintf("/usr/local/bin/horizon-container update %d", containerIdx))
} else {
outBytes, errBytes = cliutils.RunCmd(nil, "/bin/sh", "-c",
fmt.Sprintf("/usr/bin/horizon-container update %d", containerIdx))
}
if errBytes != nil && len(errBytes) > 0 {
return fmt.Errorf(msgPrinter.Sprintf("Error during restarting the anax container: %s", errBytes))
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/deb/horizon-cli/etc/horizon/hzn.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"HZN_EXCHANGE_URL": "",
"HZN_FSS_CSSURL": "",
"HZN_AGBOT_URL": ""
"HZN_AGBOT_URL": "",
"HORIZON_URL": ""
}

3 changes: 2 additions & 1 deletion pkg/rpm/horizon-cli/fs/etc/horizon/hzn.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"HZN_EXCHANGE_URL": "",
"HZN_FSS_CSSURL": "",
"HZN_AGBOT_URL": ""
"HZN_AGBOT_URL": "",
"HORIZON_URL": ""
}

0 comments on commit 2472d40

Please sign in to comment.