Skip to content
This repository has been archived by the owner on Oct 30, 2019. It is now read-only.

Commit

Permalink
Merge branch 'dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Landrash committed Mar 7, 2018
2 parents e7eaf1d + ef3fdf2 commit f11b554
Show file tree
Hide file tree
Showing 3 changed files with 241 additions and 4 deletions.
200 changes: 200 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
# Contributing to Hassbian-scripts

Everybody is invited and welcome to contribute to Hassbian-scripts.

The process is straight-forward.
- Read [How to get faster PR reviews](https://github.com/kubernetes/community/blob/master/contributors/guide/pull-requests.md#best-practices-for-faster-reviews) by Kubernetes (but skip step 0)
- Fork the Hassbian-scripts [git repository](https://github.com/home-assistant/hassbian-scripts).
- Add a new branch to your fork with a name that describe what you are implementing.
- Add or change the code.
- Ensure tests work.
- Ensure tests work. _Finding out during review that this does not work, will **not** result in a good review._
- Create a Pull Request against the [**dev**](https://github.com/home-assistant/hassbian-scripts/tree/dev) branch of Hassbian-scripts.

## Pull Requests for new scripts.
All new script must have meet the following criteria to even be reviewed:
- Stickler-Ci should report no errors. (this is an automated review process based on [shellcheck](https://github.com/koalaman/shellcheck)
- The Script must be tested with success locally, see [testing your code](#testing-your-code) for tips on how to test.
- Every script should have an validation at the end, se [validation](#validation) for tips.
- You **must** add [documentation](#documentation) to the /docs for the script.

### PR Naming
Create a good name for your PR, this will be used in the changelog.
**Good names**
- Suite: Added support for feature X.
- Hassbian-config: Added function X.
- New install script for suite.
- Suite: Fixed typo in function X.
- Suite docs: Added more information about suite.

**Bad names**
- Updated suite.sh
- Fixed typo.

### Description in the PR
Remember that it is people that are reviewing your PR, pepole that most likly don't share your mindset.
A good description of what the PR does, will certanly help during the review prosess.

### Comments
Your PR will most likly get comments during the review prosess, this is _not_ to criticise your work.
But feeback on how your PR can better match our "standards", you should have a look at exsiting scripts in the [repo](https://github.com/home-assistant/hassbian-scripts/tree/dev/package/opt/hassbian/suites).
If some comments are unclear to you, use the thread under that comment to get clarification, or drop a line in the #devs_hassbian channel over at [Discord](https://discord.gg/c5DvZ4e), we want to help you help us getting Hassbian-scripts better.

## Structure of the hassbian-scripts
The scripts in Hassbian-scripts are referred to as suites, these suites are bash scripts with an `.sh` file extension.
Each suite is built up of functions, and every script should have at least these functions:
- suite-show-short-info.
- This info will be printed at the start when the script runs and is also used by `hassbian-config show`.
- This will typically include a short description of the suit.
- suite-show-long-info.
- This info will be printed when running `hassbian-config show suite`.
- This will typically include a longer description of the suit, and it's features.
- suite-show-copyright-info.
- This info will be printed at the start when the script runs and is also used by `hassbian-config show suite`.
- This will typically include the name/username and a link to github of the person writing the script.
- suite-install-package and or suite-upgrade-package, this is where the magic happens, this is where you include your script.

## Spesial notations about install/upgrade functions
### User inputs
If your script require user inputs, they should be at the top of the function.
And if possible have an option to use `--accept (-Y)` flag, to set default values and omit the input.
Example:
```bash
function suite-install-package {
if [ "$ACCEPT" == "true" ]; then #This will be true if the suite is run with `--accept`
SUITE_USERNAME="pi"
else
echo
echo "Please take a moment to setup the suite."
echo -n "Enter a username of your choosing: "
read -r SUITE_USERNAME
if [ ! "$SUITE_USERNAME" ]; then
SUITE_USERNAME="pi" #Sets default if blank input is given.
fi
echo
fi
```
### Validation.
There are multiple ways of validating if the script was successful, these are some examples:
**Service**
This will check if there is a service with the name `shellinaboxd` running.
```bash
validation=$(pgrep -f shellinaboxd)
if [ ! -z "${validation}" ]; then
echo
echo -e "\\e[32mInstallation done..\\e[0m"
echo
echo "You can now access the web terminal here: http://$ip_address:4200"
echo "You can also add this to your Home-Assistant config in an 'panel_iframe'"
echo
else
echo
echo -e "\\e[31mInstallation failed..."
echo -e "\\e[31mAborting..."
echo -e "\\e[0mIf you have issues with this script, please say something in the #devs_hassbian channel on Discord."
echo
return 1
fi
return 0
```
**pip package**
This will check if the pip package `cython` is installed in the virtual environment.
```bash
echo "Checking the installation..."
validation=$(sudo -u homeassistant -H /bin/bash << EOF | grep Version | awk '{print $2}'
source /srv/homeassistant/bin/activate
pip3 show cython
EOF
)
if [ ! -z "${validation}" ]; then
echo
echo -e "\\e[32mInstallation done..\\e[0m"
echo
echo "To continue have a look at https://home-assistant.io/components/tradfri/"
echo "It's recommended that you restart your Tradfri Gateway before continuing."
echo
else
echo
echo -e "\\e[31mInstallation failed..."
echo -e "\\e[31mAborting..."
echo -e "\\e[0mIf you have issues with this script, please say something in the #devs_hassbian channel on Discord."
echo
return 1
fi
return 0
```
**Command line tool**
This will check if `psql` is a valid command.
```bash
validation=$(which psql)
if [ ! -z "${validation}" ]; then
echo
echo -e "\\e[32mInstallation done..\\e[0m"
echo
echo "No database or database user is created during this setup and will need to be created manually."
echo
echo "To continue have a look at https://home-assistant.io/components/recorder/"
echo
else
echo
echo -e "\\e[31mInstallation failed..."
echo -e "\\e[31mAborting..."
echo -e "\\e[0mIf you have issues with this script, please say something in the #devs_hassbian channel on Discord."
echo
return 1
fi
return 0
```
**Config change**
This example will check the value, if blank it will print "Installation Failed"
```bash
validation=$(getcap /usr/bin/python3.5 | awk -F'= ' '{print $NF}')
if [ ! -z "${validation}" ]; then
echo
echo -e "\\e[32mInstallation done..\\e[0m"
echo
echo "To continue have a look at https://home-assistant.io/components/emulated_hue/"
echo
else
echo
echo -e "\\e[31mInstallation failed..."
echo -e "\\e[31mAborting..."
echo -e "\\e[0mIf you have issues with this script, please say something in the #devs_hassbian channel on Discord."
echo
return 1
fi
return 0
```
This line should only be printed it the validation fails.
`"If you have issues with this script, please say something in the #devs_hassbian channel on Discord."`
## Testing your code
Testing the code can be done in the folowing steps:
1. Make sure you have the newest version from the upstream dev. branch. `sudo hassbian-config upgrade hassbian-script-dev`
2. Put your `suite.sh` file in the `/opt/hassbian/suites/` directory.
3. Run test with `sudo hassbian-config install suite` and/or `sudo hassbian-config upgrade suite`
- If you added support for `-y` test this to.
## Documentation
First create a new `suite.md` file in the /docs directory in your fork.
There can never be too much documentation, the file should have a minimum of:
- Description
- Installation and/or upgrade line/lines
- Who made the script.
It should also contains if possible:
- Log location
- Configuration location.
- Service commands (start, stop, restart, status)
- Defaults:
- username
- password
- port
When the `suite.md` is finished, add it as an link in the README.md
1 change: 1 addition & 0 deletions docs/hassbian_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ where command is one of:
Optional flags:
- `-y | --accept` This will accept defaults on scripts that allow this.
- `-f | --force` This will force run an script. This is useful if you need to reinstall a package.
- `-D | --debug` This will output every comand to the console.

Other available commands:
- `-V | --version` This will show you the installed version of `hassbian-config`.
Expand Down
44 changes: 40 additions & 4 deletions package/usr/local/bin/hassbian-config
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function help {
printf "available optional [options]:\\n"
printf "%-10s\\t%s\\n" " -y | --accept" "Accept defaults on scripts that allow this"
printf "%-10s\\t%s\\n" " -f | --force" "Force run an script, this is useful if you need to reinstall a package"
printf "%-10s\\t%s\\n" " -D | --debug" "This will output every comand to the console."
printf "\\n"
printf "other [command] available:\\n"
printf "%-10s\\t%s\\n" " -V | --version" "Prints the version of hassbian-config"
Expand Down Expand Up @@ -80,17 +81,36 @@ function check-permission {
}

function share-log {
if [ ! -f $LOGFILE ];then
echo "No logfile, exiting..."
return 0
fi
if [ "$ACCEPT" == "true" ]; then
a=$(cat $LOGFILE); curl -X POST -s -d "$a" https://hastebin.com/documents | awk -F '"' '{print "https://hastebin.com/"$4}'
loglink=$(curl -X POST -s -d "$(cat "$LOGFILE")" https://hastebin.com/documents | awk -F '"' '{print "https://hastebin.com/"$4}')
if [[ $loglink != *"initial"* ]]; then
echo "$loglink"
else
echo "There is an issue with your network connection, or with the Hastebin API."
echo "Try again later."
return 0
fi
else

echo "This will put the output from your last operation on hastebin."
echo "This could include sensitive information."
echo "If you are unsure about what it contains, you can run 'hassbian-config log' to check."
echo -n "Do you want to create an hastebin link? [N/y] : "
read -r RESPONSE
if [ "$RESPONSE" == "y" ] || [ "$RESPONSE" == "Y" ]; then
echo ""
a=$(cat $LOGFILE); curl -X POST -s -d "$a" https://hastebin.com/documents | awk -F '"' '{print "https://hastebin.com/"$4}'
loglink=$(curl -X POST -s -d "$(cat "$LOGFILE")" https://hastebin.com/documents | awk -F '"' '{print "https://hastebin.com/"$4}')
if [[ $loglink != *"initial"* ]]; then
echo "$loglink"
else
echo
echo "There is an issue with your network connection, or with the Hastebin API."
echo "Try again later."
return 0
fi
fi
fi
return 0
Expand All @@ -101,6 +121,9 @@ function install-suite {
echo "This script must be run with sudo. Use 'sudo hassbian-config install $1'" 1>&2
return 1
fi
if [ "$DEBUG" == "true" ]; then
set -x
fi
if [ ! -f $SUITE_CONTROL_DIR/"$1" ]; then
touch $SUITE_CONTROL_DIR/"$1"
echo "SCRIPTSTATE=uninstalled" > $SUITE_CONTROL_DIR/"$1"
Expand All @@ -117,7 +140,7 @@ function install-suite {
echo "Upgrade script is not available..."
echo "You can force run the install script like this:"
echo "sudo hassbian-config -f install $1"
exit
return 0
fi
"$1"-upgrade-package | tee $LOGFILE
sed -i -- 's/SCRIPTSTATE='"$SUITESTATE"'/SCRIPTSTATE=installed/g' $SUITE_CONTROL_DIR/"$1"
Expand All @@ -126,6 +149,9 @@ function install-suite {
"$1"-install-package | tee $LOGFILE
sed -i -- 's/SCRIPTSTATE='"$SUITESTATE"'/SCRIPTSTATE=installed/g' $SUITE_CONTROL_DIR/"$1"
fi
if [ "$DEBUG" == "true" ]; then
set +x
fi
return 0
}

Expand All @@ -134,6 +160,9 @@ function upgrade-suite {
echo "This script must be run with sudo. Use 'sudo hassbian-config upgrade $1'" 1>&2
return 1
fi
if [ "$DEBUG" == "true" ]; then
set -x
fi
UPGRADE=$(grep "$1"-upgrade-package $SUITE_INSTALL_DIR/"$1".sh)
if [ "$UPGRADE" == "" ]; then
echo "Upgrade script is not available..."
Expand All @@ -143,6 +172,9 @@ function upgrade-suite {
check-permission
source $SUITE_INSTALL_DIR/"$1".sh
"$1"-upgrade-package | tee $LOGFILE
if [ "$DEBUG" == "true" ]; then
set +x
fi
return 0
}

Expand Down Expand Up @@ -189,6 +221,10 @@ case $COMMAND in
ACCEPT="true"
shift # past argument
;;
"-D"|"--debug")
DEBUG="true"
shift # past argument
;;
"show")
if [ "$SUITE" != "" ]; then
if verify-suite "$SUITE"; then
Expand Down

0 comments on commit f11b554

Please sign in to comment.