Skip to content

Commit

Permalink
feat: update to v0.2.0; fix some path, and validation (#5)
Browse files Browse the repository at this point in the history
* PR branches check

* patch: fix logger and try to fix issues with pyenv env

* add explanation to add pyenv PATH; use py3.7 for all odoo version; add validation to check pyenv

Co-authored-by: Akhmad Maulana Akbar <[email protected]>
  • Loading branch information
rockavoldy and Akhmad Maulana Akbar authored Oct 13, 2022
1 parent ccef8e7 commit 76968d8
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 54 deletions.
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ jobs:
zip odoo-one-click_arm64.zip odoo-one-click
rm -f odoo-one-click
- name: Delete old prerelease tag with v0.x.x
if: (env.OLD_PRE_TAG == env.BUMP_TAG) && (env.BUMP_TAG != 'NULL')
uses: dev-drprasad/[email protected]
Expand Down
13 changes: 8 additions & 5 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ func init() {

var initCmd = &cobra.Command{
Use: "init",
Short: "First initialization, install pyenv, configure postgresql and clone odoo",
Long: "First initialization for installing pyenv, configure postgresql, and clone odoo",
Short: "First initialization to install pyenv, and configure postgresql",
Long: "First initialization to install pyenv, and configure postgresql for local development",
Run: func(cmd *cobra.Command, args []string) {
CheckRequirement()
checkPyenv()
},
}

func CheckRequirement() {
// Check requirement for ubuntu and derivatives
fmt.Println("Checking requirement")
// first, need to confirm if it is ubuntu or derivatives
listOfDeps := []string{"postgresql", "postgresql-client", "libxml2-dev", "libxslt1-dev", "libldap2-dev", "libsasl2-dev", "libtiff5-dev", "libjpeg8-dev", "libopenjp2-7-dev", "zlib1g-dev", "libfreetype6-dev", "liblcms2-dev", "libwebp-dev", "libharfbuzz-dev", "libpq-dev", "git", "libsqlite3-dev", "libreadline-dev", "libbz2-dev", "tk-dev"}

listOfDeps := []string{"build-essential", "postgresql", "postgresql-client", "libxml2-dev", "libssl-dev", "libffi-dev", "libxslt1-dev", "libldap2-dev", "libsasl2-dev", "libtiff5-dev", "libjpeg8-dev", "libopenjp2-7-dev", "zlib1g-dev", "libfreetype6-dev", "liblcms2-dev", "libwebp-dev", "libharfbuzz-dev", "libpq-dev", "git", "libsqlite3-dev", "libreadline-dev", "libbz2-dev", "tk-dev"}

notInstalledDeps := make([]string, 0)

Expand Down Expand Up @@ -92,7 +93,9 @@ func CheckRequirement() {
fmt.Println("Database successfully configured")
}

fmt.Printf("User database %s with password %s now have superuser access\n", config.DBUsername(), config.DB_PASSWORD)
fmt.Printf("Database user '%s' with password '%s' created with superuser access\n", config.DBUsername(), config.DB_PASSWORD)

utils.PyenvInfoBash()
}

func checkDBAccess() (bool, error) {
Expand Down
48 changes: 29 additions & 19 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func init() {

var installCmd = &cobra.Command{
Use: "install [flags] directory_name",
Short: "Install and configure odoo",
Short: "clone and configure odoo",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
return fmt.Errorf("too many arguments")
Expand All @@ -41,6 +41,19 @@ var installCmd = &cobra.Command{

return nil
},
PreRun: func(cmd *cobra.Command, args []string) {
// Check first if pyenv already installed
if !utils.IsPyenvInstalled() {
fmt.Println("Please install pyenv first.")
os.Exit(1)
}
// Check if pyenv already configured
if !utils.IsPyenvConfigured() {
fmt.Println("Please follow following steps to configure pyenv, and run the command again.")
utils.PyenvInfoBash()
os.Exit(1)
}
},
Run: func(cmd *cobra.Command, args []string) {
if isEnterprise {
// when enterprise is checked, ask for github username and token
Expand Down Expand Up @@ -125,24 +138,29 @@ func (ic InstallConf) InstallOdoo() {
Logger.Println("Initialize pyenv: ", err)
}

fmt.Println("Installing requirements")
err = ic.installOdooDeps()
if err != nil {
Logger.Println("Install odoo dependencies: ", err)
}

fmt.Println("Creating database")
err = exec.Command("createdb", ic.dbName).Run()
if err != nil {
Logger.Println("CreateDB odoo: ", err)
}

fmt.Println("Create odoo.conf file to run odoo")
err = ic.createOdooConf()
if err != nil {
Logger.Println("Create odoo conf: ", err)
}

dirName := utils.DirName(ic.odooVer, ic.isEnterprise)
fmt.Printf("Your odoo %s is ready to use at\n%s\n", dirName, config.OdooDir()+"/"+ic.dirName)
fmt.Println("for the first run, you need to initialize the db with base addons by add -i base")
fmt.Printf("Your odoo %s is ready to use at\n%s\n\n", dirName, config.OdooDir()+"/"+ic.dirName)
fmt.Println("for the first run, you need to initialize the db with base addons run this command")
fmt.Println("python3 odoo-bin -c odoo.conf -i base --stop-after-init")
fmt.Printf("\nNow you can run the odoo instance with 'python3 odoo-bin -c odoo.conf'\n")
}

func (ic InstallConf) cloneOdooCommunity() error {
Expand Down Expand Up @@ -187,31 +205,36 @@ func (ic InstallConf) cloneOdooEnterprise() error {

func (ic InstallConf) initPyenv() error {
Logger.Println("Initializing pyenv")
fmt.Println("Check if python version needed by odoo already installed by pyenv")
isPyVerInstalled, err := utils.CheckPythonInstalled(ic.pythonVer)
if err != nil {
Logger.Println(err)
}

if !isPyVerInstalled {
fmt.Println("Installing python version needed by odoo, please wait...")
err := exec.Command("pyenv", "install", ic.pythonVer).Run()
if err != nil {
return err
}
}
fmt.Printf("python %s already installed with pyenv\n", ic.pythonVer)

isVenvCreated, err := utils.CheckVenvCreated(ic.dirName)
if err != nil {
Logger.Println(err)
}

if !isVenvCreated {
fmt.Println("Creating virtual environment for odoo, please wait...")
err = exec.Command("pyenv", "virtualenv", ic.pythonVer, ic.dirName).Run()
if err != nil {
Logger.Println("Error on create venv: ", err)
return err
}
}

fmt.Println("Activating virtual environment for odoo, please wait...")
err = exec.Command("pyenv", "local", ic.dirName).Run()
if err != nil {
return err
Expand All @@ -222,6 +245,8 @@ func (ic InstallConf) initPyenv() error {

func (ic InstallConf) installOdooDeps() error {
Logger.Println("Installing Odoo Dependencies")
_ = exec.Command("pip", "install", "setuptools==57.5", "wheel").Run()

err := exec.Command("pip", "install", "-r", "requirements.txt").Run()
if err != nil {
return err
Expand All @@ -238,22 +263,7 @@ func (ic InstallConf) installOdooDeps() error {

func (ic InstallConf) createOdooConf() error {
Logger.Println("Creating Odoo Configuration")
confFile := fmt.Sprintf(`
[options]
admin_passwd = admin
db_host = localhost
db_port = 5432
db_user = %s
db_password = %s
db_name = %s
addons_path = ./addons, ./odoo/addons`, config.DBUsername(), config.DB_PASSWORD, ic.dbName)

if ic.isEnterprise {
// When it's enterprise, add enterprise addons path to odoo.conf
confFile = confFile + ", ./enterprise\n"
} else {
confFile = confFile + "\n"
}
confFile := utils.OdooConf(ic.isEnterprise, config.DBUsername(), config.DB_PASSWORD, ic.dbName)

err := os.WriteFile("odoo.conf", []byte(confFile), 0644)
if err != nil {
Expand Down
8 changes: 0 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"odoo-one-click/config"
"odoo-one-click/utils"
"os"
"os/exec"

"github.com/spf13/cobra"
)
Expand All @@ -21,13 +20,6 @@ var rootCmd = &cobra.Command{
TraverseChildren: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
Logger = utils.Logger(config.Verbose)
os.Setenv("PYENV_ROOT", config.PyenvBin())
os.Setenv("PATH", fmt.Sprintf("%s:%s", config.PyenvBin(), os.Getenv("PATH")))
installed, _ := isPyenvInstalled()
if installed {
exec.Command("eval", "$(pyenv init -)").Run()
exec.Command("eval", "$(pyenv virtualenv-init -)").Run()
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
Expand Down
20 changes: 8 additions & 12 deletions cmd/run.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package cmd

import (
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(runCmd)
// rootCmd.AddCommand(runCmd)
}

var runCmd = &cobra.Command{
Use: "run",
Short: "Run odoo instance",
Run: func(cmd *cobra.Command, args []string) {

},
}
// var runCmd = &cobra.Command{
// Use: "run",
// Short: "Run odoo instance",
// Run: func(cmd *cobra.Command, args []string) {
// fmt.Println("Run will be here")
// },
// }
2 changes: 1 addition & 1 deletion config/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func OdooDir() string {
return home + "/odoo"
}

func PyenvBin() string {
func PyenvDir() string {
home, _ := os.UserHomeDir()
return home + "/.pyenv"
}
Expand Down
71 changes: 63 additions & 8 deletions utils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package utils
import (
"fmt"
"log"
"odoo-one-click/config"
"os"
"os/exec"
"strconv"
"strings"
)

Expand Down Expand Up @@ -32,7 +33,6 @@ func DirName(odooVer string, isEnterprise bool) string {
}

func CheckPythonInstalled(pythonVer string) (bool, error) {
log.Println("Check python version if it already innstalled by pyenv")
bashCommand := fmt.Sprintf("pyenv versions | grep %s", pythonVer)
out, err := exec.Command("bash", "-c", bashCommand).Output()
if err != nil {
Expand All @@ -44,7 +44,6 @@ func CheckPythonInstalled(pythonVer string) (bool, error) {
}

func CheckVenvCreated(venv string) (bool, error) {
log.Println("Check if virtualenv with the same name already created")
bashCommand := fmt.Sprintf("pyenv virtualenvs | grep %s", venv)
out, err := exec.Command("bash", "-c", bashCommand).Output()
if err != nil {
Expand All @@ -56,12 +55,15 @@ func CheckVenvCreated(venv string) (bool, error) {
}

func GetPythonBasedOdooVer(odooVer string) string {
ver, _ := strconv.Atoi(strings.Split(odooVer, ".")[0])
if ver < 13 {
return "3.7.13"
}
// Because of some issue with py3.8 (gevent, cython, etc)
// it's better to use python3.7 for odoo 11.0 to 16.0
return "3.7.13"
// ver, _ :=÷ strconv.Atoi(strings.Split(odooVer, ".")[0])
// if ver < 13 {
// return "3.7.13"
// }

return "3.8.13"
// return "3.8.13"
}

func RemoveNewLine(data string) string {
Expand All @@ -84,3 +86,56 @@ func IsValidDirName(dirName string) bool {

return true
}

func OdooConf(isEnterprise bool, dbUser, dbPass, dbName string) string {
// [options]
// admin_passwd = admin
// db_host = localhost
// db_port = 5432
// db_user = %s
// db_password = %s
// db_name = %s
// addons_path = ./addons, ./odoo/addons

confFile := fmt.Sprintf(`
[options]
admin_passwd = admin
db_host = localhost
db_port = 5432
db_user = %s
db_password = %s
db_name = %s
addons_path = ./addons, ./odoo/addons`, dbUser, dbPass, dbName)
if isEnterprise {
confFile += ", ./enterprise\n"
} else {
confFile += "\n"
}

return confFile
}

func PyenvInfoBash() {
fmt.Println()
fmt.Println("One more thing you need to do, please add this line to your ~/.bashrc file:")
fmt.Println("(Just copy and paste to your terminal line per line)")
fmt.Println()
fmt.Println(`echo 'PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc`)
fmt.Println(`echo 'PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc`)
fmt.Println(`echo 'eval "$(pyenv init -)"' >> ~/.bashrc`)
fmt.Println(`echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc`)
fmt.Println()
fmt.Println("Then run this command to reload your bashrc file")
fmt.Println("source ~/.bashrc")
fmt.Printf("\n\nIf you have zsh as your shell, changes '~/.bashrc' here with '~/.zshrc'\n\n")
}

func IsPyenvConfigured() bool {
_, err := exec.LookPath("pyenv")
return err == nil
}

func IsPyenvInstalled() bool {
_, err := os.Stat(config.PyenvDir())
return err == nil
}

0 comments on commit 76968d8

Please sign in to comment.