summaryrefslogtreecommitdiff
path: root/posts/2014-01-11-ansible-role-for-bittorrent-sync.org
blob: 8313c809305fa88b24bfaf111003b3c27eef7df5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
Recently, I've started to look at the services I'm using and I'm trying
to find alternatives for some of them. One of them was
[[https://www.dropbox.com/][Dropbox]]. I've been using this service for
4 or 5 years at this point, and I was a paid customer for at least 3
years. My plan was about to renew in a few days, and I decided to look
for a free (either as "doesn't cost a buck" or OSS) alternative. To make
it quick, since it's not the purpose of this post, I've decided to go
with Bittorrent Sync. Still, this solution is not perfect, so here's a
short list of pros and cons:

-  Pros:

   -  Free
   -  The only limit on storage is based on the free space you have
   -  Decentralized

-  Cons

   -  Harder to set up
   -  Slower than Dropbox for syncing files
   -  Decentralized (yes I know): you need to always have at least one
      node up if you want to get a copy of a file

I decided to set btsync on one of my server that is always up, that I
way I can access my copies whenever I want from any devices (this is
similar to Dropbox). To do that I've create a set of tasks for Ansible,
following the instructions provided by
[[https://github.com/tuxpoldo][Leo Moll]] (who created the package for
Debian) on
[[http://forum.bittorrent.com/topic/18974-debian-and-ubuntu-server-packages-for-bittorrent-sync-121-1/][this
forum]].

I've created a role named 'btsync' that contains a handler, some
templates and a bunch of tasks. Let's start with the handler, since this
one is really simple. For this role, the only y service we care about is
the bittorrent sync daemon:

#+BEGIN_EXAMPLE
    - name: restart btsync
      service: name=btsync state=restarted
#+END_EXAMPLE

The tasks are pretty straightforward. First we need to fetch Leo's PGP
key so we can install the package using =apt=.

#+BEGIN_EXAMPLE
    - name: Install GPG key for btsync
      apt_key:
        id=6BF18B15
        state=present
        url=http://stinkfoot.org:11371/pks/lookup?op=get&search=0x40FC0CD26BF18B15
#+END_EXAMPLE

Now that we have the PGP key, we can add the repository to our sources
and install the package:

#+BEGIN_EXAMPLE
    - name: Add the deb repo for btsync
      apt_repository:
        repo='deb http://debian.yeasoft.net/btsync wheezy main'
        state=present

    - name: Install btsync
      apt: pkg=btsync state=installed
#+END_EXAMPLE

For the remaining tasks, we need some configuration.

#+BEGIN_EXAMPLE
    btsync_shared:
      - dir: /path/to/shared/photos
        secret: a-secret
        use_relay_server: true
        use_dht: false
        search_lan: false
        use_sync_trash: true
      - dir: /path/to/shared/documents
        secret: another-secret
        use_relay_server: true
        search_lan: false
        use_sync_trash: true
#+END_EXAMPLE

The daemon expect all the directories that it will write to, to exist.
So let's create them for him, and also set the right permissions on
them:

#+BEGIN_EXAMPLE
    - name: Create the directories where we need to sync
      file: path={{ item.dir }} state=directory owner={{ main_user_name }} group={{ main_user_name }} mode=0700
      with_items: btsync_shared
#+END_EXAMPLE

We need a specific configuration file for our user:

#+BEGIN_EXAMPLE
    - name: Configure btsync
      template:
        src=etc_btsync_user.conf.j2
        dest=/etc/btsync/{{ main_user_name }}.conf
        group={{ main_user_name }}
        owner={{ main_user_name }}
        mode=0600
      notify: restart btsync
#+END_EXAMPLE

and the template that goes with it:

#+BEGIN_EXAMPLE
    {
            "device_name": "{{ domain }}",
            "storage_path" : "/home/{{ main_user_name }}/.btsync",
            "listening_port" : 12589,
            "check_for_updates" : false,
            "use_upnp" : false,
            "download_limit" : 0,
            "upload_limit" : 0,
            "disk_low_priority" : true,
            "lan_encrypt_data" : true,
            "lan_use_tcp" : false,
            "rate_limit_local_peers" : false,
            "folder_rescan_interval" : 600,
            "shared_folders" : {{ btsync_shared | to_json }}
    }
#+END_EXAMPLE

To complete the setup, we need to tell the daemon to start with our
newly created configuration:

#+BEGIN_SRC sh
    # This is the configuration file for /etc/init.d/btsync
    #
    # Start only these btsync instances automatically via
    # init script.

    AUTOSTART="{{ main_user_name }}"

    # Optional arguments to btsync's command line. Be careful!
    # You should only add thngs here if you know EXACTLY what
    # you are doing!
    DAEMON_ARGS=""
#+END_SRC

Finally, the task to setup the default configuration for the daemon:

#+BEGIN_EXAMPLE
    - name: Configure btsync server
      template:
        src=etc_default_btsync.j2
        dest=/etc/default/btsync
        group=root
        owner=root
      notify: restart btsync
#+END_EXAMPLE

That's it!

I'll try to find some time this weekend to upload this to
[[https://galaxy.ansibleworks.com/][Galaxy]].