Browse Source

Beginning of an application developers guide

Bob Mottram 8 years ago
parent
commit
0b54295106
1 changed files with 234 additions and 0 deletions
  1. 234
    0
      doc/EN/devguide.org

+ 234
- 0
doc/EN/devguide.org View File

@@ -0,0 +1,234 @@
1
+#+TITLE:
2
+#+AUTHOR: Bob Mottram
3
+#+EMAIL: bob@robotics.uk.to
4
+#+KEYWORDS: freedombox, debian, beaglebone, red matrix, email, web server, home server, internet, censorship, surveillance, social network, irc, jabber
5
+#+DESCRIPTION: Turn the Beaglebone Black into a personal communications server
6
+#+OPTIONS: ^:nil toc:nil
7
+#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="freedombone.css" />
8
+
9
+#+BEGIN_CENTER
10
+[[file:images/logo.png]]
11
+#+END_CENTER
12
+
13
+#+begin_export html
14
+<center><h1>Developers Guide</h1></center>
15
+#+end_export
16
+
17
+* Adding extra apps
18
+Suppose you have some internet application which you want to add to the system. To do this you need to create an app script which tells the system how to install/remove and also backup/restore. On an installed system the app scripts go into the directory:
19
+
20
+#+begin_src bash
21
+/usr/share/freedombone/apps
22
+#+end_src
23
+
24
+and within the project repo they appear within the /src/ directory. Your new app script should have the name:
25
+
26
+#+begin_src bash
27
+freedombone-app-[myappname]
28
+#+end_src
29
+
30
+The /myappname/ value should not contain any spaces and will appear in the list of available apps.
31
+
32
+An example template for an app script is shown below. Copy this and add whatever variables and configuration you need. Search and replace /myappname/ with your own.
33
+
34
+#+begin_src bash
35
+#!/bin/bash
36
+# Copyright (C) Year YourName <YourEmail>
37
+#
38
+# This program is free software: you can redistribute it and/or modify
39
+# it under the terms of the GNU Affero General Public License as published by
40
+# the Free Software Foundation, either version 3 of the License, or
41
+# (at your option) any later version.
42
+#
43
+# This program is distributed in the hope that it will be useful,
44
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
45
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
46
+# GNU Affero General Public License for more details.
47
+#
48
+# You should have received a copy of the GNU Affero General Public License
49
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
50
+
51
+VARIANTS='full'
52
+
53
+SOME_IMPORTANT_CONFIG_VARIABLE='some important value'
54
+ANOTHER_IMPORTANT_CONFIG_VARIABLE='foo'
55
+MY_FUNKY_AVATAR=fro.png
56
+MYAPPNAME_ONION_PORT=[port number]
57
+MYAPPNAME_DB_PASSWORD=
58
+
59
+# A directory where the data exists
60
+MYAPP_DATA_DIR=/var/lib/somedirectory
61
+
62
+myappname_variables=(ONION_ONLY
63
+                     MY_USERNAME
64
+                     SOME_IMPORTANT_CONFIG_VARIABLE
65
+                     ANOTHER_IMPORTANT_CONFIG_VARIABLE
66
+                     MY_FUNKY_AVATAR
67
+                     MYAPPNAME_ONION_PORT
68
+                     MYAPPNAME_DB_PASSWORD)
69
+
70
+function change_password_myappname {
71
+    PASSWORD_USERNAME="$1"
72
+    PASSWORD_NEW="$2"
73
+    # Do something to change the password
74
+}
75
+
76
+function reconfigure_myappname {
77
+    echo -n ''
78
+    # Do something to delete existing keys/identity and generate new ones
79
+}
80
+
81
+function upgrade_myappname {
82
+    echo -n ''
83
+    # Do something to upgrade this app.
84
+    # If it's a debian package then it will be maintained by the
85
+    # operating system and you don't need anything here
86
+}
87
+
88
+function backup_local_myappname {
89
+    # If your app has a MariaDB/MySQL database
90
+    backup_database_to_usb myappname
91
+
92
+    # To backup a directory
93
+    backup_directory_to_usb $MYAPP_DATA_DIR myappname
94
+
95
+    # if you need to backup data within individual user home directories
96
+    for d in /home/*/ ; do
97
+        USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
98
+        if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
99
+            echo $"Backing up myappname config for $USERNAME"
100
+            if [ -d /home/$USERNAME/.config/myappname ]; then
101
+                backup_directory_to_usb /home/$USERNAME/.config/myappname myappname_users/$USERNAME
102
+            fi
103
+        fi
104
+    done
105
+}
106
+
107
+function restore_local_myappname {
108
+    temp_restore_dir=/root/tempmyappname
109
+
110
+    # If your app has a MariaDB/MySQL database
111
+    restore_database myappname
112
+
113
+    # Restore some data from a directory
114
+    # Note that we don't restore directly but to a temporary directory
115
+    # and then copy the files. This ensures that if there is a restore
116
+    # failure you don't end up with half-copied or corrupted files
117
+    restore_directory_from_usb $MYAPP_DATA_DIR myappname
118
+    cp -r $temp_restore_dir/$MYAPP_DATA_DIR $MYAPP_DATA_DIR
119
+    rm -rf $temp_restore_dir
120
+
121
+    # If you need to restore a configuration directory for each user
122
+    if [ -d $USB_MOUNT/backup/myappname_users ]; then
123
+        for d in $USB_MOUNT/backup/myappname_users/*/ ; do
124
+            USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
125
+            if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
126
+                if [ ! -d /home/$USERNAME ]; then
127
+                    ${PROJECT_NAME}-adduser $USERNAME
128
+                fi
129
+                echo $"Restoring Vim config for $USERNAME"
130
+                function_check restore_directory_from_usb
131
+                restore_directory_from_usb $temp_restore_dir myappname_users/$USERNAME
132
+                cp -r $temp_restore_dir/home/$USERNAME/.config /home/$USERNAME/
133
+                if [ ! "$?" = "0" ]; then
134
+                    rm -rf $temp_restore_dir
135
+                    set_user_permissions
136
+                    backup_unmount_drive
137
+                    exit 664
138
+                fi
139
+                rm -rf $temp_restore_dir
140
+            fi
141
+        done
142
+    fi
143
+}
144
+
145
+function backup_remote_myappname {
146
+    # this should be the same as backup_local_myappname,
147
+    # but call the backup functions backup_directory_to_friend
148
+    # and backup_database_to_friend
149
+}
150
+
151
+function restore_remote_vim {
152
+    # this should be the same as restore_local_myappname,
153
+    # but call the restore function restore_directory_from_friend
154
+    # and restore_database_from_friend
155
+}
156
+
157
+function remove_myappname {
158
+    # if it's a debian package then:
159
+    apt-get -y remove --purge [my-app-package-name]
160
+
161
+    # If your app has a MariaDB/MySQL database
162
+    drop_database myappname
163
+
164
+    # If your app uses an onion address
165
+    remove_onion_service myappname ${MYAPPNAME_ONION_PORT}
166
+}
167
+
168
+function install_myappname {
169
+    # if it's a debian package then:
170
+    apt-get -y install [my-app-package-name]
171
+
172
+    # If you need to create a MariaDB/MySQL database for the app
173
+    MYAPPNAME_DB_PASSWORD="$(create_password 20)"
174
+    create_database myappname "$MYAPPNAME_DB_PASSWORD" $MY_USERNAME
175
+
176
+    # If you need to create an onion address for the app
177
+    MYAPPNAME_ONION_HOSTNAME=$(add_onion_service myappname 80 ${MYAPPNAME_ONION_PORT})
178
+
179
+    # Do any other configuration
180
+    # Here you might use $ONION_ONLY or $SOME_IMPORTANT_CONFIG_VARIABLE
181
+
182
+    # Mark the app as having installed successfully
183
+    # If this variable isn't set then it will be assumed that
184
+    # the install has failed
185
+    APP_INSTALLED=1
186
+}
187
+
188
+function install_interactive_myappname {
189
+    # Interactively obtain some values using dialog, such as
190
+    # domain names. An avatar changing example is:
191
+    data=$(tempfile 2>/dev/null)
192
+    trap "rm -f $data" 0 1 2 5 15
193
+    dialog --title $"Change your avatar" \
194
+           --backtitle $"Freedombone Control Panel" \
195
+           --inputbox $"Enter a URL for an image. It should be approximately a square image." 8 75 2>$data
196
+    sel=$?
197
+    case $sel in
198
+        0)
199
+            MY_FUNKY_AVATAR=$(<$data)
200
+            if [ ${#MY_FUNKY_AVATAR} -gt 3 ]; then
201
+                clear
202
+
203
+                # do whatever is needed to change the avatar in your app
204
+
205
+                dialog --title $"Change your avatar" \
206
+                       --msgbox $"Your avatar has been changed" 6 40
207
+            fi
208
+            ;;
209
+    esac
210
+
211
+    # Then do the main install
212
+    install_myappname
213
+}
214
+
215
+# NOTE: deliberately no exit 0
216
+#+end_src
217
+
218
+To test your app log into your system, select *Exit to command line* then gain root powers with:
219
+
220
+#+begin_src bash
221
+sudo su
222
+#+end_src
223
+
224
+Copy your app script to */usr/share/freedombone/apps/freedombone-app-myappname*.
225
+
226
+And run the admin control panel:
227
+
228
+#+begin_src bash
229
+control
230
+#+end_src
231
+
232
+Select *Add/Remove Apps* and if all is well then you should see your app listed as installable. Test that installing and removing it works as expected.
233
+
234
+Submit your working app to *https://github.com/bashrc/freedombone/issues*