Skip to content

Commit

Permalink
More work on update
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulh committed Nov 6, 2023
1 parent b46a235 commit 5f35407
Show file tree
Hide file tree
Showing 18 changed files with 426 additions and 55 deletions.
22 changes: 21 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,24 @@ test: ## Run the tests of the project

coverage: ## Run the tests of the project and export the coverage
$(GOTEST) -cover -covermode=count -coverprofile=profile.cov ./...
$(GOCMD) tool cover -func profile.cov
$(GOCMD) tool cover -func profile.cov

install-lib: $(SUBDIRS)-install

$(SUBDIRS)-install:
$(MAKE) -C $(@:-install=) install

install: install-lib ## Install the binaries
install -Dm755 bin/$(BINARY_NAME) $(DESTDIR)$(PREFIX)/bin/$(BINARY_NAME)
install -Dm755 bin/$(BINARY_NAME_TOOL) $(DESTDIR)$(PREFIX)/bin/$(BINARY_NAME_TOOL)
install -Dm755 scripts/start_calaos_home.sh $(DESTDIR)$(PREFIX)/sbin/start_calaos_home.sh
install -Dm755 scripts/calaos_install.sh $(DESTDIR)$(PREFIX)/sbin/calaos_install.sh
install -Dm755 scripts/calaos_rollback.sh $(DESTDIR)$(PREFIX)/sbin/calaos_rollback.sh
install -Dm755 scripts/init_calaosfs.sh $(DESTDIR)$(PREFIX)/sbin/init_calaosfs.sh
install -Dm755 scripts/haproxy_pre.sh $(DESTDIR)$(PREFIX)/sbin/haproxy_pre.sh
install -Dm755 scripts/mosquitto_pre.sh $(DESTDIR)$(PREFIX)/sbin/mosquitto_pre.sh
install -Dm755 scripts/zigbee2mqtt_pre.sh $(DESTDIR)$(PREFIX)/sbin/zigbee2mqtt_pre.sh
install -Dm755 scripts/load_containers_cache.sh $(DESTDIR)$(PREFIX)/sbin/load_containers_cache.sh
install -Dm755 scripts/arch-chroot $(DESTDIR)$(PREFIX)/sbin/arch-chroot
install -Dm755 scripts/genfstab $(DESTDIR)$(PREFIX)/sbin/genfstab
install -Dm755 scripts/start_z2mqtt.sh $(DESTDIR)$(PREFIX)/sbin/start_z2mqtt.sh
12 changes: 12 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ func NewApp() (a *AppServer, err error) {
return a.apiUpdateImages(c)
})

api.Post("/update/upgrade-all", func(c *fiber.Ctx) error {
return a.apiUpdateUpgradeAll(c)
})

api.Post("/update/upgrade", func(c *fiber.Ctx) error {
return a.apiUpdateUpgrade(c)
})

api.Get("/update/status", func(c *fiber.Ctx) error {
return a.apiUpdateStatus(c)
})

return
}

Expand Down
42 changes: 42 additions & 0 deletions app/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,45 @@ func (a *AppServer) apiUpdateImages(c *fiber.Ctx) (err error) {

return c.Status(fiber.StatusOK).JSON(m)
}

func (a *AppServer) apiUpdateUpgradeAll(c *fiber.Ctx) (err error) {

err = models.UpgradeAll()
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": true,
"msg": err.Error(),
})
}

return c.Status(fiber.StatusOK).JSON(fiber.Map{
"msg": "Upgrade done",
})
}

func (a *AppServer) apiUpdateUpgrade(c *fiber.Ctx) (err error) {

err = models.Upgrade(c.Query("package"))
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": true,
"msg": err.Error(),
})
}

return c.Status(fiber.StatusOK).JSON(fiber.Map{
"msg": "Upgrade done",
})
}

func (a *AppServer) apiUpdateStatus(c *fiber.Ctx) (err error) {
s, err := models.UpdateStatus()
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": true,
"msg": err.Error(),
})
}

return c.Status(fiber.StatusOK).JSON(s)
}
1 change: 1 addition & 0 deletions apt/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.PHONY: libcalaos-apt.so

all:
@rm -fr build
meson setup build --prefix=/usr
meson compile -C build
meson install -C build
4 changes: 2 additions & 2 deletions apt/apt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Pkg *aptCacheArrayGet(void *arr, int idx)
PkgList *plist = reinterpret_cast<PkgList *>(arr);
if (!plist) return nullptr;

if (idx < 0 || idx > plist->size())
if (idx < 0 || (size_t)idx > plist->size())
return nullptr;

return &plist->at(idx);
Expand All @@ -117,7 +117,7 @@ void aptCacheArrayFree(void *arr)
PkgList *plist = reinterpret_cast<PkgList *>(arr);
if (!plist) return;

for (int i = 0;i < plist->size();i++)
for (size_t i = 0;i < plist->size();i++)
{
free((*plist)[i].name);
free((*plist)[i].version_current);
Expand Down
56 changes: 49 additions & 7 deletions cmd/calaos-os/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"net/url"
"time"

"github.com/calaos/calaos-container/models/images"
"github.com/calaos/calaos-container/models/structs"
)

const (
Expand Down Expand Up @@ -182,14 +182,14 @@ func (capi *CalaosApi) makeHttpCall(
}

// UpdateCheck forces an update check
func (capi *CalaosApi) UpdateCheck(token string) (imgs *images.ImageMap, err error) {
func (capi *CalaosApi) UpdateCheck(token string) (imgs *structs.ImageMap, err error) {

_, body, err := capi.callWithToken("GET", "/api/update/check", token, nil, nil)
if err != nil {
return
}

imgs = &images.ImageMap{}
imgs = &structs.ImageMap{}
if err = json.Unmarshal(body, imgs); err != nil {
return nil, fmt.Errorf("UpdateCheck failed: %v", err)
}
Expand All @@ -198,14 +198,14 @@ func (capi *CalaosApi) UpdateCheck(token string) (imgs *images.ImageMap, err err
}

// UpdateAvailable returns available updates
func (capi *CalaosApi) UpdateAvailable(token string) (imgs *images.ImageMap, err error) {
func (capi *CalaosApi) UpdateAvailable(token string) (imgs *structs.ImageMap, err error) {

_, body, err := capi.callWithToken("GET", "/api/update/available", token, nil, nil)
if err != nil {
return
}

imgs = &images.ImageMap{}
imgs = &structs.ImageMap{}
if err = json.Unmarshal(body, imgs); err != nil {
return nil, fmt.Errorf("UpdateAvailable failed: %v", err)
}
Expand All @@ -214,17 +214,59 @@ func (capi *CalaosApi) UpdateAvailable(token string) (imgs *images.ImageMap, err
}

// UpdateImages returns currently installed images
func (capi *CalaosApi) UpdateImages(token string) (imgs *images.ImageMap, err error) {
func (capi *CalaosApi) UpdateImages(token string) (imgs *structs.ImageMap, err error) {

_, body, err := capi.callWithToken("GET", "/api/update/images", token, nil, nil)
if err != nil {
return
}

imgs = &images.ImageMap{}
imgs = &structs.ImageMap{}
if err = json.Unmarshal(body, imgs); err != nil {
return nil, fmt.Errorf("UpdateImages failed: %v", err)
}

return
}

// UpgradePackages upgrades all packages
func (capi *CalaosApi) UpgradePackages(token string) (err error) {

_, _, err = capi.callWithToken("POST", "/api/update/upgrade-all", token, nil, nil)
if err != nil {
return
}

return
}

// UpdatePackage upgrades a single package
func (capi *CalaosApi) UpdatePackage(token string, pkg string) (err error) {

params := url.Values{
"package": []string{pkg},
}

_, _, err = capi.callWithToken("POST", "/api/update/upgrade", token, params, nil)
if err != nil {
return
}

return
}

// UpgradeStatus returns the status of the upgrade
func (capi *CalaosApi) UpgradeStatus(token string) (status *structs.Status, err error) {

_, body, err := capi.callWithToken("GET", "/api/update/status", token, nil, nil)
if err != nil {
return
}

status = &structs.Status{}
if err = json.Unmarshal(body, status); err != nil {
return nil, fmt.Errorf("UpgradeStatus failed: %v", err)
}

return
}
65 changes: 62 additions & 3 deletions cmd/calaos-os/calaos-os.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"fmt"
"os"
"strings"
"time"

"github.com/calaos/calaos-container/cmd/calaos-os/api"
"github.com/fatih/color"
cli "github.com/jawher/mow.cli"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/raoulh/go-progress"
)

const (
Expand Down Expand Up @@ -96,7 +98,7 @@ func cmdList(cmd *cli.Cmd) {
func cmdCheck(cmd *cli.Cmd) {
cmd.Spec = ""
cmd.Action = func() {
fmt.Printf("%s Checking for updates...\n", CharArrow)
fmt.Printf("%s Checking for updates...\n", cyan(CharArrow))
a := api.NewCalaosApi(api.CalaosCtHost)

token, err := getToken()
Expand All @@ -110,7 +112,7 @@ func cmdCheck(cmd *cli.Cmd) {
}

if len(*imgs) == 0 {
fmt.Printf("%s Already up to date.\n", CharCheck)
fmt.Printf("%s Already up to date.\n", green(CharCheck))
return
}

Expand All @@ -135,7 +137,64 @@ func cmdCheck(cmd *cli.Cmd) {
}

func cmdUpgrade(cmd *cli.Cmd) {
cmd.Spec = ""
cmd.Spec = "[PKG]"
pkg := cmd.StringArg("PKG", "", "Package to upgrade. If not specified, all packages will be updated")

cmd.Action = func() {
if (*pkg) != "" {
fmt.Printf("%s Upgrading package %s...\n", cyan(CharArrow), *pkg)
} else {
fmt.Printf("%s Upgrading all packages...\n", cyan(CharArrow))
}

a := api.NewCalaosApi(api.CalaosCtHost)

token, err := getToken()
if err != nil {
exit(err, 1)
}

if (*pkg) != "" {
err = a.UpdatePackage(token, *pkg)
} else {
err = a.UpgradePackages(token)
}

//get status from API and wait until it returns idle
bar := progress.New(100)
bar.Format = progress.ProgressFormats[0]
bar.ShowNumeric = false
bar.ShowTextSuffix = true

currentPkg := ""

for {
status, err := a.UpgradeStatus(token)
if err != nil {
exit(err, 1)
}
if status.Status == "idle" {
break
}

if currentPkg != status.CurrentPkg {
bar.SetTextSuffix(fmt.Sprintf("\t %s %s installed", green(CharCheck), status.CurrentPkg))
bar.Set(100)
fmt.Println()

currentPkg = status.CurrentPkg
}

bar.SetTextSuffix(fmt.Sprintf("\t Installing %s", status.CurrentPkg))
bar.Set(status.Progress * 100 / status.ProgressTotal)

time.Sleep(500 * time.Millisecond)
}

if err != nil {
exit(err, 1)
}

fmt.Printf("%s Done.\n", green(CharCheck))
}
}
15 changes: 1 addition & 14 deletions debian/calaos-container.install
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
bin/calaos-container usr/bin
bin/calaos-os usr/bin
scripts/start_calaos_home.sh usr/bin
calaos-container.toml etc
env/calaos.sh etc/profile.d
scripts/calaos_install.sh usr/sbin
scripts/calaos_rollback.sh usr/sbin
scripts/init_calaosfs.sh usr/sbin
scripts/haproxy_pre.sh usr/sbin
scripts/mosquitto_pre.sh usr/sbin
scripts/zigbee2mqtt_pre.sh usr/sbin
scripts/load_containers_cache.sh usr/sbin
scripts/arch-chroot usr/sbin
scripts/genfstab usr/sbin
initramfs/hooks/grub-btrfs-overlay usr/share/initramfs-tools/hooks/grub-btrfs-overlay
initramfs/scripts/local-bottom/grub-btrfs-overlay usr/share/initramfs-tools/scripts/local-bottom/grub-btrfs-overlay
grub-btrfs/41_snapshots-btrfs etc/grub.d
Expand All @@ -20,5 +8,4 @@ grub-btrfs/grub-btrfsd usr/bin
repository/calaos.gpg etc/apt/trusted.gpg.d
repository/calaos.list etc/apt/sources.list.d
debian/[email protected] lib/systemd/system
debian/[email protected] lib/systemd/system
scripts/start_z2mqtt.sh usr/bin
debian/[email protected] lib/systemd/system
2 changes: 1 addition & 1 deletion debian/calaos-home.service
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Type=notify
NotifyAccess=all
SyslogIdentifier=%N

ExecStart=/usr/bin/start_calaos_home.sh %t/%N.cid
ExecStart=/usr/sbin/start_calaos_home.sh %t/%N.cid

RestartSec=2

Expand Down
2 changes: 1 addition & 1 deletion debian/zigbee2mqtt.service
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Delegate=yes
Type=notify
NotifyAccess=all
SyslogIdentifier=%N
ExecStart=/usr/bin/start_z2mqtt.sh %t/%N.cid
ExecStart=/usr/sbin/start_z2mqtt.sh %t/%N.cid

[Install]
WantedBy=multi-user.target
Loading

0 comments on commit 5f35407

Please sign in to comment.