aboutsummaryrefslogtreecommitdiffhomepage
path: root/apps/peertube-cli/src/peertube-redundancy.ts
blob: 56fc6366b291a14cd318f9060c5e63a34c40cfb4 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import bytes from 'bytes'
import CliTable3 from 'cli-table3'
import { URL } from 'url'
import { Command } from '@commander-js/extra-typings'
import { forceNumber, uniqify } from '@peertube/peertube-core-utils'
import { HttpStatusCode, VideoRedundanciesTarget } from '@peertube/peertube-models'
import { assignToken, buildServer, CommonProgramOptions, getServerCredentials } from './shared/index.js'

export function defineRedundancyProgram () {
  const program = new Command()
    .name('redundancy')
    .description('Manage instance redundancies')
    .alias('r')

  program
    .command('list-remote-redundancies')
    .description('List remote redundancies on your videos')
    .option('-u, --url <url>', 'Server url')
    .option('-U, --username <username>', 'Username')
    .option('-p, --password <token>', 'Password')
    .action(async options => {
      try {
        await listRedundanciesCLI({ target: 'my-videos', ...options })
      } catch (err) {
        console.error('Cannot list remote redundancies: ' + err.message)
        process.exit(-1)
      }
    })

  program
    .command('list-my-redundancies')
    .description('List your redundancies of remote videos')
    .option('-u, --url <url>', 'Server url')
    .option('-U, --username <username>', 'Username')
    .option('-p, --password <token>', 'Password')
    .action(async options => {
      try {
        await listRedundanciesCLI({ target: 'remote-videos', ...options })
      } catch (err) {
        console.error('Cannot list redundancies: ' + err.message)
        process.exit(-1)
      }
    })

  program
    .command('add')
    .description('Duplicate a video in your redundancy system')
    .option('-u, --url <url>', 'Server url')
    .option('-U, --username <username>', 'Username')
    .option('-p, --password <token>', 'Password')
    .requiredOption('-v, --video <videoId>', 'Video id to duplicate', parseInt)
    .action(async options => {
      try {
        await addRedundancyCLI(options)
      } catch (err) {
        console.error('Cannot duplicate video: ' + err.message)
        process.exit(-1)
      }
    })

  program
    .command('remove')
    .description('Remove a video from your redundancies')
    .option('-u, --url <url>', 'Server url')
    .option('-U, --username <username>', 'Username')
    .option('-p, --password <token>', 'Password')
    .requiredOption('-v, --video <videoId>', 'Video id to remove from redundancies', parseInt)
    .action(async options => {
      try {
        await removeRedundancyCLI(options)
      } catch (err) {
        console.error('Cannot remove redundancy: ' + err)
        process.exit(-1)
      }
    })

  return program
}

// ----------------------------------------------------------------------------

async function listRedundanciesCLI (options: CommonProgramOptions & { target: VideoRedundanciesTarget }) {
  const { target } = options

  const { url, username, password } = await getServerCredentials(options)
  const server = buildServer(url)
  await assignToken(server, username, password)

  const { data } = await server.redundancy.listVideos({ start: 0, count: 100, sort: 'name', target })

  const table = new CliTable3({
    head: [ 'video id', 'video name', 'video url', 'files', 'playlists', 'by instances', 'total size' ]
  }) as any

  for (const redundancy of data) {
    const webVideoFiles = redundancy.redundancies.files
    const streamingPlaylists = redundancy.redundancies.streamingPlaylists

    let totalSize = ''
    if (target === 'remote-videos') {
      const tmp = webVideoFiles.concat(streamingPlaylists)
        .reduce((a, b) => a + b.size, 0)

      // FIXME: don't use external dependency to stringify bytes: we already have the functions in the client
      totalSize = bytes(tmp)
    }

    const instances = uniqify(
      webVideoFiles.concat(streamingPlaylists)
        .map(r => r.fileUrl)
        .map(u => new URL(u).host)
    )

    table.push([
      redundancy.id.toString(),
      redundancy.name,
      redundancy.url,
      webVideoFiles.length,
      streamingPlaylists.length,
      instances.join('\n'),
      totalSize
    ])
  }

  console.log(table.toString())
}

async function addRedundancyCLI (options: { video: number } & CommonProgramOptions) {
  const { url, username, password } = await getServerCredentials(options)
  const server = buildServer(url)
  await assignToken(server, username, password)

  if (!options.video || isNaN(options.video)) {
    throw new Error('You need to specify the video id to duplicate and it should be a number.')
  }

  try {
    await server.redundancy.addVideo({ videoId: options.video })

    console.log('Video will be duplicated by your instance!')
  } catch (err) {
    if (err.message.includes(HttpStatusCode.CONFLICT_409)) {
      throw new Error('This video is already duplicated by your instance.')
    }

    if (err.message.includes(HttpStatusCode.NOT_FOUND_404)) {
      throw new Error('This video id does not exist.')
    }

    throw err
  }
}

async function removeRedundancyCLI (options: CommonProgramOptions & { video: number }) {
  const { url, username, password } = await getServerCredentials(options)
  const server = buildServer(url)
  await assignToken(server, username, password)

  if (!options.video || isNaN(options.video)) {
    throw new Error('You need to specify the video id to remove from your redundancies')
  }

  const videoId = forceNumber(options.video)

  const myVideoRedundancies = await server.redundancy.listVideos({ target: 'my-videos' })
  let videoRedundancy = myVideoRedundancies.data.find(r => videoId === r.id)

  if (!videoRedundancy) {
    const remoteVideoRedundancies = await server.redundancy.listVideos({ target: 'remote-videos' })
    videoRedundancy = remoteVideoRedundancies.data.find(r => videoId === r.id)
  }

  if (!videoRedundancy) {
    throw new Error('Video redundancy not found.')
  }

  const ids = videoRedundancy.redundancies.files
    .concat(videoRedundancy.redundancies.streamingPlaylists)
    .map(r => r.id)

  for (const id of ids) {
    await server.redundancy.removeVideo({ redundancyId: id })
  }

  console.log('Video redundancy removed!')
}