Skip to content
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

Go tutorial and reference #723

Open
wants to merge 61 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
e74cf0c
Go tutorial and reference
javierdelapuente Oct 2, 2024
90de295
Merge branch 'main' into go-tutorial
javierdelapuente Oct 2, 2024
5e9bcc8
Run pipeline
javierdelapuente Oct 3, 2024
f00280e
minor improvements to the go docs
javierdelapuente Oct 3, 2024
26731a0
Merge branch 'main' into go-tutorial
javierdelapuente Oct 3, 2024
c454a84
Add --fail to all curl commands
javierdelapuente Oct 4, 2024
cd58c67
Merge branch 'go-tutorial' of github.com:javierdelapuente/rockcraft i…
javierdelapuente Oct 4, 2024
6215c1d
tutorial fixes
javierdelapuente Oct 4, 2024
db4666b
Replace second part of the tutorial with a new endpoint
javierdelapuente Oct 4, 2024
42166ce
Add missing space
javierdelapuente Oct 4, 2024
cbce355
update to ubuntu 24.04
javierdelapuente Oct 4, 2024
c810f0f
minor corrections
javierdelapuente Oct 4, 2024
4a97758
shorten line
javierdelapuente Oct 4, 2024
87db8a2
Fixes and improvements
javierdelapuente Oct 8, 2024
73b9e13
Merge branch 'main' into go-tutorial
javierdelapuente Oct 8, 2024
8e4f4b3
Apply suggestions from code review
javierdelapuente Oct 9, 2024
fb1062a
Merge branch 'main' into go-tutorial
javierdelapuente Oct 9, 2024
a529914
Remove unneeded go mod tidy
javierdelapuente Oct 9, 2024
5527e67
Run ci
javierdelapuente Oct 9, 2024
a4fc3d1
Replace you with we and remove Ubuntu version dependency
javierdelapuente Oct 10, 2024
f1a3f93
go mod init before creating the main.go file
javierdelapuente Oct 10, 2024
5784740
go-framework and typo
javierdelapuente Oct 10, 2024
19d83ea
Run docker as a regular user
javierdelapuente Oct 10, 2024
db56bc7
Link to pebble docs in the first occurrence
javierdelapuente Oct 10, 2024
3c8603c
quote sudo
javierdelapuente Oct 10, 2024
e0ba7da
intermediate cleanup -> stop the application
javierdelapuente Oct 10, 2024
6c7da80
add next steps part
javierdelapuente Oct 10, 2024
d845408
Merge branch 'main' into go-tutorial
javierdelapuente Oct 10, 2024
0d69b10
typo
javierdelapuente Oct 10, 2024
12750a2
Merge branch 'main' into go-tutorial
javierdelapuente Oct 14, 2024
4f61ced
Merge branch 'main' into go-tutorial
javierdelapuente Oct 15, 2024
e84fe5f
Apply suggestions from code review
javierdelapuente Oct 17, 2024
7a92aa6
Merge branch 'main' into go-tutorial
javierdelapuente Oct 17, 2024
b7f5653
update link as the other tutorials
javierdelapuente Oct 18, 2024
b22fabf
suggestions from code review
javierdelapuente Oct 22, 2024
1e9d175
Merge branch 'main' into go-tutorial
javierdelapuente Oct 23, 2024
16eb379
Update docs/reference/extensions/go-framework.rst
javierdelapuente Oct 23, 2024
79d8765
Merge branch 'main' into go-tutorial
javierdelapuente Oct 24, 2024
7aca5d7
Merge branch 'main' into go-tutorial
javierdelapuente Nov 7, 2024
d5fb848
Suggestion for rewording about the http server create by the go app
javierdelapuente Nov 12, 2024
4e5f93f
Merge branch 'main' into go-tutorial
javierdelapuente Nov 14, 2024
ef88a60
Update docs/tutorial/go.rst
javierdelapuente Nov 14, 2024
b83026d
Update docs/tutorial/go.rst
javierdelapuente Nov 14, 2024
6993978
Update docs/tutorial/go.rst
javierdelapuente Nov 14, 2024
e61d3df
Update docs/tutorial/go.rst
javierdelapuente Nov 14, 2024
1e343c5
Update docs/tutorial/go.rst
javierdelapuente Nov 14, 2024
c6759cd
Update docs/tutorial/go.rst
javierdelapuente Nov 14, 2024
c548bd2
Update docs/tutorial/go.rst
javierdelapuente Nov 14, 2024
a686246
Update docs/tutorial/go.rst
javierdelapuente Nov 14, 2024
f48f04a
Update docs/tutorial/go.rst
javierdelapuente Nov 14, 2024
82746c9
Update docs/tutorial/go.rst
javierdelapuente Nov 14, 2024
f489aeb
Update docs/tutorial/go.rst
javierdelapuente Nov 14, 2024
581a7c3
Update docs/tutorial/go.rst
javierdelapuente Nov 14, 2024
5a27628
Update docs/tutorial/go.rst
javierdelapuente Nov 14, 2024
2a8e6a8
Update docs/tutorial/go.rst
javierdelapuente Nov 14, 2024
4666ce7
Address comments in code review
javierdelapuente Nov 14, 2024
a0b3214
Update docs/tutorial/go.rst
javierdelapuente Nov 14, 2024
5960832
Update docs/tutorial/go.rst
javierdelapuente Nov 14, 2024
c0c3293
experimental in the beginning of the tutorial. minor rearrangement
javierdelapuente Nov 16, 2024
60a8503
Add information about the estimated time and skills required acquired
javierdelapuente Nov 16, 2024
72dd047
remove note on amd64 and the bare base
javierdelapuente Nov 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions docs/reference/extensions/go-framework.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
.. _go-framework-reference:

go-framework
----------------

The Go extension streamlines the process of building Go application
rocks.

The extension builds and copies the Go binary file to the rock,
inside the ``/app`` directory. By default, the base ``bare`` is used,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the final image, it looks like the binary is in /usr/local/bin and /app is empty though.

$ docker run hello:0.1 ls /app
$ docker run hello:0.1 ls /usr/local/bin
hello

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, /app has the assets, not the binaries. Removed that sentence and updated the part of the assets to say that they will be in the /app directory.

that generates a lightweight image.
javierdelapuente marked this conversation as resolved.
Show resolved Hide resolved


.. note::
The Go extension is compatible with the ``bare`` and ``[email protected]``
bases.

Project requirements
====================

To use the ``go-framework`` extension, there must be a ``go.mod`` file
in the root directory of the project.


``parts`` > ``go-framework/install-app`` > ``organize``
=========================================================

If the main package is in the base directory and the rockcraft name
attribute is equal to the go module name, the name of the binary will
be selected correctly, otherwise you can adjust it.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otherwise you can adjust it.

Seems like I HAVE to change it with that part, otherwise it doesn't build.

go.mod
module foo

go 1.23.0
rockcraft.yaml
name: hello
version: '0.2' 
summary: TODO
description: TODO

base: bare 
build-base: [email protected] 
platforms:
    amd64:
    
extensions:
    - go-framework
Failure logs
Starting rockcraft, version 1.6.0
Logging execution to '/tmp/rockcraft.log'
*EXPERIMENTAL* extension 'go-framework' enabled
Extracting bare:latest
Extracted bare:latest
Initialising lifecycle
Installing build-packages
Installing build-snaps
Skipping pull for go-framework/base-layout (already ran)
Updating sources for go-framework/install-app (source changed)
Skipping pull for go-framework/runtime (already ran)
Skipping pull for pebble (already ran)
Reapplying go-framework/base-layout (previous layer changed)
Reapplying go-framework/install-app (previous layer changed)
Reapplying go-framework/runtime (previous layer changed)
Reapplying pebble (previous layer changed)
Skipping build for go-framework/base-layout (already ran)
Rebuilding go-framework/install-app ('organize' property changed)
:: + go mod download all
:: + go install -p 4 ./...
Skipping build for go-framework/runtime (already ran)
Skipping build for pebble (already ran)
Skipping stage for go-framework/base-layout (already ran)
Staging go-framework/install-app
/root/parts/go-framework/install-app/install/usr: No such file or directory
Failed to execute rockcraft in instance.
Full execution log: '/home/rough/.local/state/rockcraft/log/rockcraft-20241022-005012.610019.log'

But if I add the parts.go-framework/install-app.organize tweak, it works.

Changed rockcraft.yaml
name: hello
version: '0.2' 
summary: TODO
description: TODO

base: bare 
build-base: [email protected] 
platforms:
    amd64:
    
extensions:
    - go-framework

parts:
  go-framework/install-app:
   organize:
     bin/foo: usr/local/bin/hello

Copy link
Contributor Author

@javierdelapuente javierdelapuente Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you can is not the best way of expressing it.... Updated to you will need.. Thanks!


You can use this field to specify a different binary to be used as the
main application, without having to override the service command. For example,
if your Go application container a ``main`` package in the directory
javierdelapuente marked this conversation as resolved.
Show resolved Hide resolved
``cmd/anotherserver``, the name of the binary will be ``anotherserver``
and you can override the main application to use the binary with the
next snippet:

.. code-block:: yaml

parts:
go-framework/install-app:
organize:
bin/anotherserver: usr/local/bin/<rockcraft project name>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would bin/<rockcraft project name> not work? :O

Copy link
Contributor Author

@javierdelapuente javierdelapuente Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bin/<rockcraft project name>: usr/local/bin/<rockcraft project name> is the "default one".

If there are several Go binaries, and we want to include several of them in the rock, someway we have to choose the main one, and it will be the one in usr/local/<rockcraft project name>, and the one you selected is located (where the go plugin puts it) in bin/thebinaryname.



``parts`` > ``go-framework/assets`` > ``stage``
=========================================================


Some files, if they exist, are included by default. These include:
``migrate``, ``migrate.sh``, ``templates/`` and ``static/``.

You can customise the files to include by overriding the ``stage`` property
of the ``go-framework/assets`` part:

.. code-block:: yaml

parts:
go-framework/assets:
stage:
- app/migrate
- app/migrate.sh
- app/static
- app/another_file_or_directory


Useful links
============

- :ref:`build-a-rock-for-a-go-application`
1 change: 1 addition & 0 deletions docs/reference/extensions/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ initiating a new rock.

flask-framework
django-framework
go-framework
3 changes: 3 additions & 0 deletions docs/reference/rockcraft.yaml.rst
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ Extensions to enable when building the ROCK.
Currently supported extensions:

- ``flask-framework``
- ``django-framework``
- ``go-framework``
- ``fastapi-framework``

Example
=======
Expand Down
4 changes: 4 additions & 0 deletions docs/tutorial/code/go/anotherserver_part
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parts:
go-framework/install-app:
organize:
bin/anotherserver: usr/local/bin/go-hello-world
18 changes: 18 additions & 0 deletions docs/tutorial/code/go/cmd/anotherserver/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"fmt"
"log"
"net/http"
)

func helloWorld(w http.ResponseWriter, req *http.Request) {
log.Printf("anotherserver hello world request")
fmt.Fprintf(w, "Hello World from anotherserver")
}

func main() {
log.Printf("starting anotherserver hello world application")
http.HandleFunc("/", helloWorld)
http.ListenAndServe(":8000", nil)
}
18 changes: 18 additions & 0 deletions docs/tutorial/code/go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"fmt"
"log"
"net/http"
)

func helloWorld(w http.ResponseWriter, req *http.Request) {
log.Printf("new hello world request")
fmt.Fprintf(w, "Hello World")
javierdelapuente marked this conversation as resolved.
Show resolved Hide resolved
}

func main() {
log.Printf("starting hello world application")
http.HandleFunc("/", helloWorld)
http.ListenAndServe(":8000", nil)
}
111 changes: 111 additions & 0 deletions docs/tutorial/code/go/task.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
###########################################
# IMPORTANT
# Comments matter!
# The docs use the wrapping comments as
# markers for including said instructions
# as snippets in the docs.
###########################################
summary: Getting started with Go tutorial

environment:

execute: |
set -x
# [docs:install-go]
sudo snap install go --classic
# [docs:install-go-end]

# [docs:mod-init-go]
go mod init go-hello-world
go mod tidy
javierdelapuente marked this conversation as resolved.
Show resolved Hide resolved
# [docs:mod-init-go-end]

# [docs:go-build]
go build .
# [docs:go-build-end]

./go-hello-world &
retry -n 5 --wait 2 curl --fail localhost:8000

# [docs:curl-go]
curl --fail localhost:8000
# [docs:curl-go-end]

kill $!

# [docs:create-rockcraft-yaml]
rockcraft init --profile go-framework
# [docs:create-rockcraft-yaml-end]
sed -i "s/name: .*/name: go-hello-world/g" rockcraft.yaml

# [docs:pack]
ROCKCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS=true rockcraft pack
# [docs:pack-end]

# [docs:ls-rock]
ls *.rock -l --block-size=MB
# [docs:ls-rock-end]

# [docs:skopeo-copy]
sudo rockcraft.skopeo --insecure-policy \
copy oci-archive:go-hello-world_0.1_amd64.rock \
docker-daemon:go-hello-world:0.1
# [docs:skopeo-copy-end]

# [docs:docker-images]
sudo docker images go-hello-world:0.1
# [docs:docker-images-end]

# [docs:docker-run]
sudo docker run --rm -d -p 8000:8000 \
--name go-hello-world go-hello-world:0.1
# [docs:docker-run-end]

retry -n 5 --wait 2 curl --fail localhost:8000
# [docs:curl-go-rock]
curl --fail localhost:8000
# [docs:curl-go-rock-end]
[ "$(curl localhost:8000)" = "Hello World" ]

# [docs:get-logs]
sudo docker exec go-hello-world pebble logs go
# [docs:get-logs-end]

# [docs:stop-docker]
sudo docker stop go-hello-world
sudo docker rmi go-hello-world:0.1
# [docs:stop-docker-end]

## Part 2 of the tutorial
sed -i "s/version: .*/version: 0.2/g" rockcraft.yaml
cat anotherserver_part >> rockcraft.yaml

# [docs:docker-run-update]
ROCKCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS=true rockcraft pack
sudo rockcraft.skopeo --insecure-policy \
copy oci-archive:go-hello-world_0.2_amd64.rock \
docker-daemon:go-hello-world:0.2
sudo docker images go-hello-world:0.2
sudo docker run --rm -d -p 8000:8000 \
--name go-hello-world go-hello-world:0.2
# [docs:docker-run-update-end]
retry -n 5 --wait 2 curl --fail localhost:8000

# [docs:curl-anotherserver]
curl --fail localhost:8000
# [docs:curl-anotherserver-end]
[ "$(curl localhost:8000)" = "Hello World from anotherserver" ]

# [docs:stop-docker-updated]
sudo docker stop go-hello-world
sudo docker rmi go-hello-world:0.2
# [docs:stop-docker-updated-end]

# [docs:cleanup]
# delete all the files created during the tutorial
rm go.mod rockcraft.yaml main.go go-hello-world \
go-hello-world_0.1_amd64.rock \
cmd/anotherserver/main.go \
go-hello-world_0.2_amd64.rock
rmdir -p cmd/anotherserver
# [docs:cleanup-end]
Loading
Loading